ocket8888 commented on a change in pull request #4379: Rewrite isos to Golang URL: https://github.com/apache/trafficcontrol/pull/4379#discussion_r382772770
########## File path: traffic_ops/traffic_ops_golang/iso/iso_test.go ########## @@ -0,0 +1,625 @@ +package iso + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import ( + "context" + "encoding/json" + "io/ioutil" + "net" + "net/http" + "net/http/httptest" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/apache/trafficcontrol/lib/go-rfc" + libtc "github.com/apache/trafficcontrol/lib/go-tc" + "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/auth" + "github.com/jmoiron/sqlx" + "gopkg.in/DATA-DOG/go-sqlmock.v1" +) + +func TestISOS(t *testing.T) { + cases := []struct { + name string // name of testcase + input isoRequest // input to isos function + cmdMod func(in *exec.Cmd) *exec.Cmd // optional modifier of the cmd that will be executed + validator func(t *testing.T, w *httptest.ResponseRecorder) // validation function which should pass/fail the test + }{ + { + name: "cmd success", + input: isoRequest{ + DHCP: boolStr{true, false}, + Stream: boolStr{true, true}, + OSVersionDir: "centos72", + HostName: "db", + DomainName: "infra.ciab.test", + IPAddr: net.IP{172, 20, 0, 4}, + InterfaceMTU: 1500, + InterfaceName: "eth0", + IP6Address: net.IP{}, + IP6Gateway: net.IP{}, + IPGateway: net.IP{172, 20, 0, 1}, + IPNetmask: net.IP{255, 255, 0, 0}, + MgmtInterface: "", + MgmtIPAddress: net.IP{}, + MgmtIPGateway: net.IP{}, + MgmtIPNetmask: net.IP{}, + Disk: "sda", + RootPass: "12345678", + }, + cmdMod: func(in *exec.Cmd) *exec.Cmd { + // Modify command such that it will use the mock command with + // a successful response. It will write to STDOUT the original + // command+args, e.g. mkisofs arg1 arg2..., which should be copied + // to the response's body. + return mockISOCmd(in, false, "") + }, + validator: func(t *testing.T, gotResp *httptest.ResponseRecorder) { + // Validate response code + if got, expected := gotResp.Code, http.StatusOK; got != expected { + t.Errorf("response status = %d; expected %d", got, expected) + } else { + t.Logf("response status = %d", got) + } + + // Validate Content-Disposition header + if got, expectedPrefix := gotResp.Header().Get(httpHeaderContentDisposition), "attachment; filename="; !strings.HasPrefix(got, expectedPrefix) { + t.Errorf("header %q = %q; expected prefix: %q", httpHeaderContentDisposition, got, expectedPrefix) + } else { + t.Logf("header %q = %q", httpHeaderContentDisposition, got) + } + + // Validate Content-Type header + if got, expected := gotResp.Header().Get(httpHeaderContentType), httpHeaderContentDownload; got != expected { + t.Errorf("header %q = %q; expected: %q", httpHeaderContentType, got, expected) + } else { + t.Logf("header %q = %q", httpHeaderContentType, got) + } + + // Because of the mocked command, the data written to the response body should be + // the command and args that were originally set to be executed. In this case, it's + // expected to be the default mkisofs command since there's no `generate` script present. + if got, expectedPrefix := gotResp.Body.String(), mkisofsBin; !strings.HasPrefix(got, expectedPrefix) { + t.Errorf("got command: %q; expected command prefix of: %q", got, expectedPrefix) + } else { + t.Logf("got command: %q", got) + } + }, + }, + + { + name: "cmd failure", + input: isoRequest{ + DHCP: boolStr{true, false}, + Stream: boolStr{true, true}, + OSVersionDir: "centos72", + HostName: "db", + DomainName: "infra.ciab.test", + IPAddr: net.IP{172, 20, 0, 4}, + InterfaceMTU: 1500, + InterfaceName: "eth0", + IP6Address: net.IP{}, + IP6Gateway: net.IP{}, + IPGateway: net.IP{172, 20, 0, 1}, + IPNetmask: net.IP{255, 255, 0, 0}, + MgmtInterface: "", + MgmtIPAddress: net.IP{}, + MgmtIPGateway: net.IP{}, + MgmtIPNetmask: net.IP{}, + Disk: "sda", + RootPass: "12345678", + }, + cmdMod: func(in *exec.Cmd) *exec.Cmd { + // Modify command such that it will return an error. + return mockISOCmd(in, true, "") + }, + validator: func(t *testing.T, gotResp *httptest.ResponseRecorder) { + // TODO: Validate response code. Because of how api.HandleErr works, it's + // not possible to see the actual response code in the response recorder. + + // Validate Content-Type header, which should be JSON for this error condition. + if got, expected := gotResp.Header().Get(httpHeaderContentType), rfc.ApplicationJSON; got != expected { + t.Errorf("header %q = %q; expected: %q", httpHeaderContentType, got, expected) + } else { + t.Logf("header %q = %q", httpHeaderContentType, got) + } + + // Validate that the response body is a JSON-encoded tc.Alerts object. + var expectedResp libtc.Alerts + if err := json.NewDecoder(gotResp.Body).Decode(&expectedResp); err != nil { + t.Fatalf("unable to decode body into expected JSON structure: %v", err) + } + + t.Logf("response: %#v", expectedResp) + }, + }, + } + + tmpDirPrefix := t.Name() + + for _, tc := range cases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + // Create the OS versions & ks_scripts directories within a temporary directory. + // The tmpDir will be used, via a Parameter entry in the database, instead of the default + // /var/www/files directory. + + tmpDir, err := ioutil.TempDir("", tmpDirPrefix) + if err != nil { + t.Fatalf("error creating tempdir: %v", err) + } + // Clean up temp dir + defer os.RemoveAll(tmpDir) + // Create expected directory structure + if err = os.MkdirAll(filepath.Join(tmpDir, tc.input.OSVersionDir, ksCfgDir), 0777); err != nil { + t.Fatal(err) + } + + // Create osversions.json file, which is read by validateOSDir method + osVersions, err := json.Marshal(libtc.OSVersionsResponse{"test": tc.input.OSVersionDir}) + if err != nil { + t.Fatal(err) + } + if err = ioutil.WriteFile(filepath.Join(tmpDir, cfgFilename), osVersions, 0777); err != nil { Review comment: I really don't like seeing files made with `0777` permissions; I think this really only needs to be `0644` (or even `0600` might work but maybe a bit overkill). ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
