// 2019 Rackspace, Inc. // // Licensed 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. // To build this app, run the following command: // CGO_ENABLED=0 GOARCH=amd64 go build -a -installsuffix cgo -o bin/change_dc change_dc.go package uglyservers import ( "os" "sort" "testing" "github.com/sirupsen/logrus" "github.rackspace.com/gorax/gokeys" ) var ( testClient *Client testDevices = []int{958309} ) // TestMain function needs to be ran at the beginning of each test file. // Since Go testing does not (and should not) rely on an execution order, the // init function ensures that the required types are initialized prior to // running your test. If they are already initialized, this will do nothing. func TestMain(m *testing.M) { logrus.Info("PRETEST: Gathering test credentials") cfg, err := gokeys.New(nil) if err != nil { logrus.Fatalf("gokeys.New:%s", err) } testClient = initCfg(cfg) exitVal := m.Run() os.Exit(exitVal) } func TestGetDeviceList(t *testing.T) { r, err := testClient.getDeviceList("IAD2-MG-09", "iad2") if err != nil { t.Fatal(err) } if len(r) == 0 { t.Fatalf("ERROR: no devices returned") } t.Logf("SUCCESS: found %d ugly servers for move-group", len(r)) } func TestGetPortMaps(t *testing.T) { r, err := testClient.GetPortMaps(testDevices) if err != nil { t.Fatal(err) } if len(r) == 0 { t.Fatalf("ERROR: no devices returned") } for k, v := range r { t.Logf("%s found port-map %d", k, len(v.PortMaps)) } t.Logf("SUCCESS: found %d port-maps", len(r)) } func TestPortMapAudit(t *testing.T) { deviceMap, err := testClient.GetPortMaps(testDevices) if err != nil { t.Fatal(err) } for device, maps := range deviceMap { if errs := testClient.PortMapAudit(maps); len(errs) != 0 { for _, e := range errs { t.Errorf("ERRORS:%s: port-map issue [%v]", device, e) } continue } t.Logf("SUCCESS:%s: no port-map issues", device) } } //nolint:scopelint func TestSort(t *testing.T) { aggExMap := map[string][]AggExMap{ "B4-9-3": []AggExMap{ {Port: 14, MappedSwitch: "A10-3-2", MappedPort: 37}, {Port: 28, MappedSwitch: "A10-3-2", MappedPort: 41}, {Port: 13, MappedSwitch: "A10-3-2", MappedPort: 8}, }, "B4-10-3": []AggExMap{ {Port: 24, MappedSwitch: "A10-4-2", MappedPort: 37}, {Port: 23, MappedSwitch: "A10-4-2", MappedPort: 26}, }, } for sw := range aggExMap { for _, k := range aggExMap[sw] { t.Logf("PRE SORT: %s[%d] => %s[%d]", sw, k.Port, k.MappedSwitch, k.MappedPort) } sort.SliceStable(aggExMap[sw], func(i, j int) bool { return aggExMap[sw][i].Port < aggExMap[sw][j].Port }) for _, k := range aggExMap[sw] { t.Logf("POST SORT: %s[%d] => %s[%d]", sw, k.Port, k.MappedSwitch, k.MappedPort) } } }