github.com/m-lab/locate@v0.17.6/siteinfo/siteinfo_test.go (about) 1 package siteinfo 2 3 import ( 4 "net/url" 5 "reflect" 6 "sort" 7 "testing" 8 9 v2 "github.com/m-lab/locate/api/v2" 10 ) 11 12 var testInstances = map[string]v2.HeartbeatMessage{ 13 "ndt-oma7777-217f832a.mlab.sandbox.measurement-lab.org": { 14 Health: &v2.Health{ 15 Score: 1, 16 }, 17 Registration: &v2.Registration{ 18 City: "Omaha", 19 CountryCode: "US", 20 ContinentCode: "NA", 21 Experiment: "ndt", 22 Hostname: "ndt-oma7777-217f832a.mlab.sandbox.measurement-lab.org", 23 Latitude: 41.3032, 24 Longitude: -95.8941, 25 Machine: "217f832a", 26 Metro: "oma", 27 Project: "mlab-sandbox", 28 Probability: 0.1, 29 Site: "oma7777", 30 Type: "unknown", 31 Uplink: "unknown", 32 Services: map[string][]string{ 33 "ndt/ndt7": { 34 "ws:///ndt/v7/download", 35 "ws:///ndt/v7/upload", 36 "wss:///ndt/v7/download", 37 "wss:///ndt/v7/upload", 38 }, 39 }, 40 }, 41 Prometheus: &v2.Prometheus{ 42 Health: true, 43 }, 44 }, 45 "ndt-dfw8888-73a354f1.testorg.sandbox.measurement-lab.org": { 46 Health: &v2.Health{ 47 Score: 1, 48 }, 49 Registration: &v2.Registration{ 50 City: "Dallas", 51 CountryCode: "US", 52 ContinentCode: "NA", 53 Experiment: "ndt", 54 Hostname: "ndt-dfw8888-73a354f1.testorg.sandbox.measurement-lab.org", 55 Latitude: 32.8969, 56 Longitude: -97.0381, 57 Machine: "73a354f1", 58 Metro: "dfw", 59 Project: "mlab-sandbox", 60 Probability: 0.1, 61 Site: "dfw8888", 62 Type: "unknown", 63 Uplink: "unknown", 64 Services: map[string][]string{ 65 "ndt/ndt7": { 66 "ws:///ndt/v7/download", 67 "ws:///ndt/v7/upload", 68 "wss:///ndt/v7/download", 69 "wss:///ndt/v7/upload", 70 }, 71 }, 72 }, 73 Prometheus: &v2.Prometheus{ 74 Health: true, 75 }, 76 }, 77 "msak-chs9999-ab285f12.mlab.sandbox.measurement-lab.org": { 78 Health: &v2.Health{ 79 Score: 1, 80 }, 81 Registration: &v2.Registration{ 82 City: "Charleston", 83 CountryCode: "US", 84 ContinentCode: "NA", 85 Experiment: "msak", 86 Hostname: "msak-chs9999-ab285f12.mlab.sandbox.measurement-lab.org", 87 Latitude: 32.8986, 88 Longitude: -80.0405, 89 Machine: "ab285f12", 90 Metro: "chs", 91 Project: "mlab-sandbox", 92 Probability: 0.5, 93 Site: "chs9999", 94 Type: "unknown", 95 Uplink: "unknown", 96 Services: map[string][]string{ 97 "ndt/ndt7": { 98 "ws:///ndt/v7/download", 99 "ws:///ndt/v7/upload", 100 "wss:///ndt/v7/download", 101 "wss:///ndt/v7/upload", 102 }, 103 }, 104 }, 105 Prometheus: &v2.Prometheus{ 106 Health: false, 107 }, 108 }, 109 } 110 111 func TestMachines(t *testing.T) { 112 tests := []struct { 113 name string 114 params url.Values 115 expectedKeys []string 116 instances map[string]v2.HeartbeatMessage 117 wantErr bool 118 }{ 119 { 120 name: "success-return-all-records", 121 instances: testInstances, 122 expectedKeys: []string{ 123 "msak-chs9999-ab285f12.mlab.sandbox.measurement-lab.org", 124 "ndt-dfw8888-73a354f1.testorg.sandbox.measurement-lab.org", 125 "ndt-oma7777-217f832a.mlab.sandbox.measurement-lab.org", 126 }, 127 }, 128 { 129 name: "success-return-mlab-records", 130 instances: testInstances, 131 params: url.Values{ 132 "org": { 133 "mlab", 134 }, 135 }, 136 expectedKeys: []string{ 137 "msak-chs9999-ab285f12.mlab.sandbox.measurement-lab.org", 138 "ndt-oma7777-217f832a.mlab.sandbox.measurement-lab.org", 139 }, 140 }, 141 { 142 name: "success-return-ndt-records", 143 instances: testInstances, 144 params: url.Values{ 145 "exp": { 146 "ndt", 147 }, 148 }, 149 expectedKeys: []string{ 150 "ndt-dfw8888-73a354f1.testorg.sandbox.measurement-lab.org", 151 "ndt-oma7777-217f832a.mlab.sandbox.measurement-lab.org", 152 }, 153 }, 154 { 155 name: "success-return-mlab-ndt-records", 156 instances: testInstances, 157 params: url.Values{ 158 "exp": { 159 "ndt", 160 }, 161 "org": { 162 "mlab", 163 }, 164 }, 165 expectedKeys: []string{ 166 "ndt-oma7777-217f832a.mlab.sandbox.measurement-lab.org", 167 }, 168 }, 169 { 170 name: "error-invalid-hostname", 171 instances: map[string]v2.HeartbeatMessage{ 172 "invalid.hostname": {}, 173 }, 174 params: url.Values{ 175 "org": { 176 "mlab", 177 }, 178 }, 179 wantErr: true, 180 }, 181 } 182 183 for _, test := range tests { 184 var resultKeys []string 185 186 result, err := Machines(test.instances, test.params) 187 if (err != nil) != test.wantErr { 188 t.Errorf("Machines() error = %v, wantErr %v", err, test.wantErr) 189 } 190 191 for k := range result { 192 resultKeys = append(resultKeys, k) 193 } 194 195 sort.Strings(resultKeys) 196 197 if !reflect.DeepEqual(test.expectedKeys, resultKeys) { 198 t.Errorf("Machines() wanted = %v, got %v", test.expectedKeys, resultKeys) 199 } 200 } 201 }