mhoppa commented on a change in pull request #3935: Rewrite POST servercheck to GO URL: https://github.com/apache/trafficcontrol/pull/3935#discussion_r328368624
########## File path: traffic_ops/testing/api/v14/serverchecks_test.go ########## @@ -0,0 +1,138 @@ +package v14 + +/* + + 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. +*/ + +import ( + "testing" + "time" + + "github.com/apache/trafficcontrol/lib/go-tc" + + "github.com/apache/trafficcontrol/lib/go-log" +) + +func TestServerChecks(t *testing.T) { + WithObjs(t, []TCObj{CDNs, Types, Parameters, Profiles, Statuses, Divisions, Regions, PhysLocations, CacheGroups, Servers, TOExtensions, ServerChecks}, func() { + CreateTestInvalidServerChecks(t) + UpdateTestServerChecks(t) + GetTestServerChecks(t) + }) +} + +func CreateTestServerChecks(t *testing.T) { + SwitchSession(toReqTimeout, Config.TrafficOps.URL, Config.TrafficOps.Users.Admin, Config.TrafficOps.UserPassword, Config.TrafficOps.Users.Extension, Config.TrafficOps.UserPassword) + + for _, servercheck := range testData.Serverchecks { + resp, _, err := TOSession.InsertServerCheckStatus(servercheck) + log.Debugf("Response: %v host_name %v check %v", *servercheck.HostName, *servercheck.Name, resp) + if err != nil { + t.Errorf("could not CREATE servercheck: %v\n", err) + } + } +} + +func CreateTestInvalidServerChecks(t *testing.T) { + toReqTimeout := time.Second * time.Duration(Config.Default.Session.TimeoutInSecs) + + SwitchSession(toReqTimeout, Config.TrafficOps.URL, Config.TrafficOps.Users.Extension, Config.TrafficOps.UserPassword, Config.TrafficOps.Users.Admin, Config.TrafficOps.UserPassword) + + _, _, err := TOSession.InsertServerCheckStatus(testData.Serverchecks[0]) + if err == nil { + t.Errorf("expected to receive error with non extension user\n") + } + + SwitchSession(toReqTimeout, Config.TrafficOps.URL, Config.TrafficOps.Users.Admin, Config.TrafficOps.UserPassword, Config.TrafficOps.Users.Extension, Config.TrafficOps.UserPassword) + + strToPtr := func(s string) *string { return &s } + intToPtr := func(i int) *int { return &i } + + invalidServerCheck := tc.ServercheckRequestNullable{ + Name: strToPtr("BOGUS"), + Value: intToPtr(1), + ID: intToPtr(-1), + HostName: strToPtr("bogus_hostname"), + } + + // Attempt to create a ServerCheck with invalid server ID + _, _, err = TOSession.InsertServerCheckStatus(invalidServerCheck) + if err == nil { + t.Errorf("expected to receive error with invalid id\n") + } + + invalidServerCheck.ID = nil + // Attempt to create a ServerCheck with invalid host name + _, _, err = TOSession.InsertServerCheckStatus(invalidServerCheck) + if err == nil { + t.Errorf("expected to receive error with invalid host name\n") + } + + // get valid name to get past host check + invalidServerCheck.Name = testData.Serverchecks[0].Name + + // Attempt to create a ServerCheck with invalid servercheck name + _, _, err = TOSession.InsertServerCheckStatus(invalidServerCheck) + if err == nil { + t.Errorf("expected to receive error with invalid servercheck name\n") + } + +} + +func UpdateTestServerChecks(t *testing.T) { + for _, servercheck := range testData.Serverchecks { + newValue := *servercheck.Value - 1 + servercheck.Value = &newValue Review comment: yes let me clean that up ---------------------------------------------------------------- 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
