package main import ( "fmt" "os" "testing" "reflect" ) // checking for a valid return value. func TestGetEnvKey(t *testing.T) { key := "TEST_VAR" val := "42" os.Setenv(key, val) have, err := getEnv(key) if have != val || err != nil { t.Fatalf(`getEnv("%s") = %s, %s, want %s, nil`, key, have, err, val) } } // checking for an error. func TestGetEnvKeyEmptyKey(t *testing.T) { have, err := getEnv("") if have != "" || err == nil { t.Fatalf(`getEnv("") = %s, %s, want "", error`, have, err) } } // checking for an error. func TestLoadConfigMissingKey(t *testing.T) { fmt.Println("") os.Setenv("API_USER", "user") os.Setenv("API_KEY", "mykey") os.Setenv("ACCESS_IP", "1.1.1.1") os.Setenv("DOMAIN_MAIN", "mysite") // NOTE: DOMAIN_END is missing. err := loadConfig() _, errWant := getEnv("DOMAIN_END") if err.Error() != errWant.Error() || err == nil { t.Fatalf(`have %v, want %v`, err, errWant) } } // NOTE: this test must happen AFTER TestLoadConfigMissingKey // checking for a valid return value. func TestLoadConfigAllKeys(t *testing.T) { os.Setenv("API_USER", "user") os.Setenv("API_KEY", "mykey") os.Setenv("ACCESS_IP", "1.1.1.1") os.Setenv("DOMAIN_MAIN", "mysite") os.Setenv("DOMAIN_END", "xyz") loadConfig() want := "https://api.namecheap.com/xml.response?ApiUser=user&ApiKey=mykey&UserName=user&ClientIp=1.1.1.1&SLD=mysite&TLD=xyz" if apiUrl != want { t.Fatalf(`apiUrl = %s, want %s`, apiUrl, want) } } // checking for a valid return value. func TestNormalizeUserChoicesCleanInput(t *testing.T) { userChoicesStrs := []string{"0", "1", "6"} maxVal := 7 want := []int{0, 1, 6} have, err := normalizeUserChoices(userChoicesStrs, maxVal) if !reflect.DeepEqual(have, want) || err != nil { t.Fatalf(`normalizeUserChoices(%v, %d) = %v, %v, want %v,nil`, userChoicesStrs, maxVal, have, err, want) } } // checking for a valid return value. func TestNormalizeUserChoicesExtraSpaces(t *testing.T) { userChoicesStrs := []string{" ", " ", "1", "", " 5 ", " "} maxVal := 7 want := []int{1, 5} have, err := normalizeUserChoices(userChoicesStrs, maxVal) if !reflect.DeepEqual(have, want) || err != nil { t.Fatalf(`normalizeUserChoices(%v, %d) = %v, %v, want %v,nil`, userChoicesStrs, maxVal, have, err, want) } } // checking for an error. func TestNormalizeUserChoicesWrongChar(t *testing.T) { userChoicesStrs := []string{"6", "5b"} maxVal := 7 errWant := "Error: Bad input detected: 5b. Please only enter numbers." have, err := normalizeUserChoices(userChoicesStrs, maxVal) if have != nil || err.Error() != errWant { t.Fatalf(`normalizeUserChoices(%v, %d) = %v, %v, want nil,%v`, userChoicesStrs, maxVal, have, err, errWant) } } // checking for an error. func TestNormalizeUserChoicesOutOfRangeLower(t *testing.T) { userChoicesStrs := []string{"-1", "5"} maxVal := 7 errWant := "Error: Bad user input detected. The number -1 is out of range." have, err := normalizeUserChoices(userChoicesStrs, maxVal) if have != nil || err.Error() != errWant { t.Fatalf(`normalizeUserChoices(%v, %d) = %v, %v, want nil,%v`, userChoicesStrs, maxVal, have, err, errWant) } } // checking for an error. func TestNormalizeUserChoicesOutOfRangeUpper(t *testing.T) { userChoicesStrs := []string{"0", "7"} maxVal := 7 errWant := "Error: Bad user input detected. The number 7 is out of range." have, err := normalizeUserChoices(userChoicesStrs, maxVal) if have != nil || err.Error() != errWant { t.Fatalf(`normalizeUserChoices(%v, %d) = %v, %v, want nil,%v`, userChoicesStrs, maxVal, have, err, errWant) } } // checking for a valid return value. func TestUpdateHostRecordSet(t *testing.T) { rec1 := Host{ Name: "@", Type: "T", Address: "foo", MXPref: 1, TTL: 1, } rec2 := Host{ Name: "www", Type: "T", Address: "bar", MXPref: 1, TTL: 1, } updatedRec2 := Host{ Name: "www", Type: "T", Address: "new", MXPref: 10, TTL: 99, } recs := []Host{rec1, rec2} want := []Host{rec1, updatedRec2} // update recs in place updateHostRecordSet(recs, updatedRec2) if !reflect.DeepEqual(recs, want) { t.Fatalf(`have %v, want %v`, recs, want) } } // checking for a valid return value. func TestUpdateHostRecordSetNoOp(t *testing.T) { rec1 := Host{ Name: "@", Type: "T", Address: "foo", MXPref: 1, TTL: 1, } rec2 := Host{ Name: "www", Type: "T", Address: "bar", MXPref: 1, TTL: 1, } updatedRec2 := Host{ Name: "www", Type: "A", Address: "new", MXPref: 10, TTL: 99, } recs := []Host{rec1, rec2} want := []Host{rec1, rec2} // update recs in place updateHostRecordSet(recs, updatedRec2) if !reflect.DeepEqual(recs, want) { t.Fatalf(`have %v, want %v`, recs, want) } }