github.com/OpsMx/go-app-base@v0.0.24/birger/controller_test.go (about) 1 // Copyright 2022 OpsMx, Inc 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 birger 16 17 import ( 18 "reflect" 19 "testing" 20 ) 21 22 func Test_parseAgentStatistics(t *testing.T) { 23 tests := []struct { 24 name string 25 data []byte 26 filter []string 27 want map[string]controllerService 28 wantErr bool 29 }{ 30 { 31 "empty errors", 32 []byte(""), 33 []string{"whoami"}, 34 map[string]controllerService{}, 35 true, 36 }, { 37 "One agent, one service, one annotation", 38 []byte(` 39 { 40 "serverTime": 1662067531436, 41 "version": "v3.4.6-6-g4eee038", 42 "connectedAgents": [ 43 { 44 "name": "smith", 45 "session": "0001HH270W7TD8DZZ6STNY2ASX", 46 "connectionType": "direct", 47 "endpoints": [ 48 { 49 "name": "whoami", 50 "type": "whoami", 51 "configured": true, 52 "annotations": { 53 "description": "demo service" 54 } 55 } 56 ], 57 "version": "v3.4.6-6-g4eee038", 58 "hostname": "studio.local", 59 "connectedAt": 1662065692965, 60 "lastPing": 1662067522916, 61 "agentInfo": { 62 "annotations": { 63 "description": "demo agent" 64 } 65 } 66 } 67 ] 68 } 69 `), 70 []string{"whoami"}, 71 map[string]controllerService{ 72 "smith:whoami:whoami": { 73 Name: "whoami", 74 Type: "whoami", 75 AgentName: "smith", 76 Annotations: map[string]string{ 77 "description": "demo service", 78 }, 79 }, 80 }, 81 false, 82 }, { 83 "most recently connected agent endpoints are used", 84 []byte(` 85 { 86 "serverTime": 1662067531436, 87 "version": "v3.4.6-6-g4eee038", 88 "connectedAgents": [ 89 { 90 "name": "smith", 91 "session": "session-one", 92 "connectionType": "direct", 93 "endpoints": [ 94 { 95 "name": "whoami", 96 "type": "whoami", 97 "configured": true, 98 "annotations": { 99 "description": "demo service", 100 "otherAnnotation": "newer annotation" 101 } 102 } 103 ], 104 "version": "v3.4.6-6-g4eee038", 105 "hostname": "studio.local", 106 "connectedAt": 999, 107 "lastPing": 1662067522916, 108 "agentInfo": { 109 "annotations": { 110 "description": "demo agent" 111 } 112 } 113 }, 114 { 115 "name": "smith", 116 "session": "session-two", 117 "connectionType": "direct", 118 "endpoints": [ 119 { 120 "name": "whoami", 121 "type": "whoami", 122 "configured": true, 123 "annotations": { 124 "description": "demo service", 125 "otherAnnotation": "very old annotation" 126 } 127 } 128 ], 129 "version": "v3.4.6-6-g4eee038", 130 "hostname": "studio.local", 131 "connectedAt": 111, 132 "lastPing": 1662067522916, 133 "agentInfo": { 134 "annotations": { 135 "description": "demo agent" 136 } 137 } 138 }, 139 { 140 "name": "smith", 141 "session": "session-two", 142 "connectionType": "direct", 143 "endpoints": [ 144 { 145 "name": "whoami", 146 "type": "whoami", 147 "configured": true, 148 "annotations": { 149 "description": "demo service", 150 "otherAnnotation": "old annotation" 151 } 152 } 153 ], 154 "version": "v3.4.6-6-g4eee038", 155 "hostname": "studio.local", 156 "connectedAt": 222, 157 "lastPing": 1662067522916, 158 "agentInfo": { 159 "annotations": { 160 "description": "demo agent" 161 } 162 } 163 } 164 ] 165 } 166 `), 167 []string{"whoami"}, 168 map[string]controllerService{ 169 "smith:whoami:whoami": { 170 Name: "whoami", 171 Type: "whoami", 172 AgentName: "smith", 173 Annotations: map[string]string{ 174 "description": "demo service", 175 "otherAnnotation": "newer annotation", 176 }, 177 }, 178 }, 179 false, 180 }, 181 } 182 for _, tt := range tests { 183 t.Run(tt.name, func(t *testing.T) { 184 m := ControllerManager{serviceTypes: tt.filter} 185 got, err := m.parseAgentStatistics(tt.data) 186 if (err != nil) != tt.wantErr { 187 t.Errorf("parseAgentStatistics() error = %v, wantErr %v", err, tt.wantErr) 188 return 189 } 190 if !reflect.DeepEqual(got, tt.want) { 191 t.Errorf("parseAgentStatistics() = %v, want %v", got, tt.want) 192 } 193 }) 194 } 195 }