github.com/google/cloudprober@v0.11.3/config/config_test.go (about) 1 // Copyright 2017 The Cloudprober Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package config 16 17 import ( 18 "fmt" 19 "testing" 20 21 "cloud.google.com/go/compute/metadata" 22 ) 23 24 func TestParse(t *testing.T) { 25 testConfig := ` 26 {{ $shard := "ig-us-east1-a-02-afgx" | extractSubstring "[^-]+-[^-]+-[^-]+-[^-]+-([^-]+)-.*" 1 }} 27 probe { 28 type: PING 29 name: "vm-to-google-{{$shard}}-{{.region}}" 30 targets { 31 host_names: "www.google.com" 32 } 33 ping_probe { 34 use_datagram_socket: true 35 } 36 } 37 ` 38 c, err := Parse(testConfig, map[string]string{ 39 "region": "testRegion", 40 }) 41 if err != nil { 42 t.Fatal(err) 43 } 44 if len(c.GetProbe()) != 1 { 45 t.Errorf("Didn't get correct number of probes. Got: %d, Expected: %d", len(c.GetProbe()), 1) 46 } 47 probeName := c.GetProbe()[0].GetName() 48 expectedName := "vm-to-google-02-testRegion" 49 if probeName != expectedName { 50 t.Errorf("Incorrect probe name. Got: %s, Expected: %s", probeName, expectedName) 51 } 52 } 53 54 func TestParseMap(t *testing.T) { 55 testConfig := ` 56 {{define "probeTmpl"}} 57 probe { 58 type: {{.typ}} 59 name: "{{.name}}" 60 targets { 61 host_names: "www.google.com" 62 } 63 ping_probe { 64 use_datagram_socket: true 65 } 66 } 67 {{end}} 68 69 {{template "probeTmpl" mkMap "typ" "PING" "name" "ping_google"}} 70 ` 71 c, err := Parse(testConfig, map[string]string{}) 72 if err != nil { 73 t.Fatal(err) 74 } 75 if len(c.GetProbe()) != 1 { 76 t.Errorf("Didn't get correct number of probes. Got: %d, Expected: %d", len(c.GetProbe()), 1) 77 } 78 probeName := c.GetProbe()[0].GetName() 79 expectedName := "ping_google" 80 if probeName != expectedName { 81 t.Errorf("Incorrect probe name. Got: %s, Expected: %s", probeName, expectedName) 82 } 83 } 84 85 func TestParseForTest(t *testing.T) { 86 testConfig := ` 87 probe { 88 type: PING 89 name: "{{gceCustomMetadata "google-probe-name"}}-{{gceCustomMetadata "cluster"}}" 90 targets { 91 host_names: "www.google.com" 92 } 93 } 94 ` 95 ReadFromGCEMetadata = func(key string) (string, error) { 96 if key == "google-probe-name" { 97 return "google_dot_com_from", nil 98 } 99 if key == "cluster" { 100 return "", metadata.NotDefinedError("not defined") 101 } 102 return "", fmt.Errorf("not-implemented") 103 } 104 105 c, err := Parse(testConfig, map[string]string{}) 106 if err != nil { 107 t.Fatal(err) 108 } 109 if len(c.GetProbe()) != 1 { 110 t.Errorf("Didn't get correct number of probes. Got: %d, Expected: %d", len(c.GetProbe()), 1) 111 } 112 probeName := c.GetProbe()[0].GetName() 113 expectedName := "google_dot_com_from-undefined" 114 if probeName != expectedName { 115 t.Errorf("Incorrect probe name. Got: %s, Expected: %s", probeName, expectedName) 116 } 117 }