github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/store/redis/workload_test.go (about) 1 package redis 2 3 import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 "time" 8 9 "github.com/projecteru2/core/types" 10 ) 11 12 func (s *RediaronTestSuite) TestAddORUpdateWorkload() { 13 ctx := context.Background() 14 id := "1234567812345678123456781234567812345678123456781234567812345678" 15 name := "test_app_1" 16 nodename := "n1" 17 podname := "test" 18 workload := &types.Workload{ 19 ID: id, 20 Nodename: nodename, 21 Podname: podname, 22 Name: "a", 23 } 24 // failed by name 25 err := s.rediaron.AddWorkload(ctx, workload, nil) 26 s.Error(err) 27 workload.Name = name 28 // fail update 29 err = s.rediaron.UpdateWorkload(ctx, workload) 30 s.Error(err) 31 // success create 32 err = s.rediaron.AddWorkload(ctx, workload, nil) 33 s.NoError(err) 34 // success updat 35 err = s.rediaron.UpdateWorkload(ctx, workload) 36 s.NoError(err) 37 } 38 39 func (s *RediaronTestSuite) TestRemoveWorkload() { 40 ctx := context.Background() 41 id := "1234567812345678123456781234567812345678123456781234567812345678" 42 name := "test_app_1" 43 nodename := "n1" 44 podname := "test" 45 workload := &types.Workload{ 46 ID: id, 47 Nodename: nodename, 48 Podname: podname, 49 Name: name, 50 } 51 // success create 52 err := s.rediaron.AddWorkload(ctx, workload, nil) 53 s.NoError(err) 54 // fail remove 55 workload.Name = "a" 56 err = s.rediaron.RemoveWorkload(ctx, workload) 57 s.Error(err) 58 workload.Name = name 59 // success remove 60 err = s.rediaron.RemoveWorkload(ctx, workload) 61 s.NoError(err) 62 } 63 64 func (s *RediaronTestSuite) TestGetWorkload() { 65 ctx := context.Background() 66 id := "1234567812345678123456781234567812345678123456781234567812345678" 67 name := "test_app_1" 68 nodename := "n1" 69 podname := "test" 70 workload := &types.Workload{ 71 ID: id, 72 Nodename: nodename, 73 Podname: podname, 74 Name: name, 75 } 76 // success create 77 err := s.rediaron.AddWorkload(ctx, workload, nil) 78 s.NoError(err) 79 // failed by no workload 80 _, err = s.rediaron.GetWorkloads(ctx, []string{id, "xxx"}) 81 s.Error(err) 82 // failed by no pod nodes 83 _, err = s.rediaron.GetWorkload(ctx, id) 84 s.Error(err) 85 // create pod node 86 _, err = s.rediaron.AddPod(ctx, podname, "") 87 s.NoError(err) 88 _, err = s.rediaron.AddNode(ctx, &types.AddNodeOptions{ 89 Nodename: nodename, 90 Endpoint: "mock://", 91 Podname: podname, 92 }) 93 s.NoError(err) 94 // success 95 _, err = s.rediaron.GetWorkload(ctx, id) 96 s.NoError(err) 97 } 98 99 func (s *RediaronTestSuite) TestGetWorkloadStatus() { 100 ctx := context.Background() 101 id := "1234567812345678123456781234567812345678123456781234567812345678" 102 name := "test_app_1" 103 nodename := "n1" 104 podname := "test" 105 workload := &types.Workload{ 106 ID: id, 107 Nodename: nodename, 108 Podname: podname, 109 Name: name, 110 } 111 // success create 112 err := s.rediaron.AddWorkload(ctx, workload, nil) 113 s.NoError(err) 114 // failed no pod no node 115 _, err = s.rediaron.GetWorkloadStatus(ctx, id) 116 s.Error(err) 117 // add success 118 _, err = s.rediaron.AddPod(ctx, podname, "") 119 s.NoError(err) 120 _, err = s.rediaron.AddNode(ctx, &types.AddNodeOptions{ 121 Nodename: nodename, 122 Endpoint: "mock://", 123 Podname: podname, 124 }) 125 s.NoError(err) 126 c, err := s.rediaron.GetWorkloadStatus(ctx, id) 127 s.Nil(c) 128 } 129 130 func (s *RediaronTestSuite) TestSetWorkloadStatus() { 131 m := s.rediaron 132 ctx := context.Background() 133 id := "1234567812345678123456781234567812345678123456781234567812345678" 134 name := "test_app_1" 135 nodename := "n1" 136 podname := "test" 137 workload := &types.Workload{ 138 ID: id, 139 Nodename: nodename, 140 Podname: podname, 141 StatusMeta: &types.StatusMeta{ID: id}, 142 } 143 // fail by no name 144 err := m.SetWorkloadStatus(ctx, workload.StatusMeta, 0) 145 s.Error(err) 146 workload.Name = name 147 workload.StatusMeta.Appname = "test" 148 workload.StatusMeta.Entrypoint = "app" 149 workload.StatusMeta.Nodename = "n1" 150 // no workload, err nil 151 err = m.SetWorkloadStatus(ctx, workload.StatusMeta, 10) 152 s.ErrorIs(err, types.ErrInvaildCount) 153 s.NoError(m.AddWorkload(ctx, workload, nil)) 154 // no status key, put succ, err nil 155 err = m.SetWorkloadStatus(ctx, workload.StatusMeta, 10) 156 s.NoError(err) 157 // status not changed, update old lease 158 err = m.SetWorkloadStatus(ctx, workload.StatusMeta, 10) 159 s.NoError(err) 160 // status changed, revoke old lease 161 workload.StatusMeta.Running = true 162 err = m.SetWorkloadStatus(ctx, workload.StatusMeta, 10) 163 s.NoError(err) 164 // status not changed, ttl = 0 165 err = m.SetWorkloadStatus(ctx, workload.StatusMeta, 0) 166 s.NoError(err) 167 } 168 169 func (s *RediaronTestSuite) TestListWorkloads() { 170 m := s.rediaron 171 ctx := context.Background() 172 // no key 173 cs, err := m.ListWorkloads(ctx, "", "a", "b", 1, nil) 174 s.NoError(err) 175 s.Empty(cs) 176 // add workload 177 name := "test_app_1" 178 nodename := "n1" 179 podname := "test" 180 id := "1234567812345678123456781234567812345678123456781234567812345678" 181 workload := &types.Workload{ 182 ID: id, 183 Nodename: nodename, 184 Podname: podname, 185 Name: name, 186 Labels: map[string]string{"x": "y"}, 187 } 188 // success create 189 err = m.AddWorkload(ctx, workload, nil) 190 s.NoError(err) 191 _, err = m.AddPod(ctx, podname, "") 192 s.NoError(err) 193 _, err = m.AddNode(ctx, &types.AddNodeOptions{ 194 Nodename: nodename, 195 Endpoint: "mock://", 196 Podname: podname, 197 }) 198 s.NoError(err) 199 // no labels 200 cs, err = m.ListWorkloads(ctx, "", "a", "b", 1, nil) 201 s.NoError(err) 202 s.NotEmpty(cs) 203 // labels 204 cs, err = m.ListWorkloads(ctx, "", "a", "b", 1, map[string]string{"x": "z"}) 205 s.NoError(err) 206 s.Empty(cs) 207 } 208 209 func (s *RediaronTestSuite) TestListNodeWorkloads() { 210 m := s.rediaron 211 ctx := context.Background() 212 // no key 213 cs, err := m.ListNodeWorkloads(ctx, "", nil) 214 s.NoError(err) 215 s.Empty(cs) 216 // add workload 217 name := "test_app_1" 218 nodename := "n1" 219 podname := "test" 220 id := "1234567812345678123456781234567812345678123456781234567812345678" 221 workload := &types.Workload{ 222 ID: id, 223 Nodename: nodename, 224 Podname: podname, 225 Name: name, 226 Labels: map[string]string{"x": "y"}, 227 } 228 // success create 229 err = m.AddWorkload(ctx, workload, nil) 230 s.NoError(err) 231 _, err = m.AddPod(ctx, podname, "") 232 s.NoError(err) 233 _, err = m.AddNode(ctx, &types.AddNodeOptions{Nodename: nodename, Endpoint: "mock://", Podname: podname}) 234 s.NoError(err) 235 // no labels 236 cs, err = m.ListNodeWorkloads(ctx, nodename, nil) 237 s.NoError(err) 238 s.NotEmpty(cs) 239 // labels 240 cs, err = m.ListNodeWorkloads(ctx, nodename, map[string]string{"x": "z"}) 241 s.NoError(err) 242 s.Empty(cs) 243 } 244 245 func (s *RediaronTestSuite) TestWorkloadStatusStream() { 246 m := s.rediaron 247 ctx := context.Background() 248 id := "1234567812345678123456781234567812345678123456781234567812345678" 249 name := "test_app_1" 250 appname := "test" 251 entrypoint := "app" 252 nodename := "n1" 253 podname := "test" 254 workload := &types.Workload{ 255 ID: id, 256 Name: name, 257 Nodename: nodename, 258 Podname: podname, 259 StatusMeta: &types.StatusMeta{ID: id}, 260 } 261 node := &types.Node{ 262 NodeMeta: types.NodeMeta{ 263 Name: nodename, 264 Podname: podname, 265 Endpoint: "tcp://127.0.0.1:2376", 266 }, 267 } 268 _, err := json.Marshal(workload) 269 s.NoError(err) 270 nodeBytes, err := json.Marshal(node) 271 s.NoError(err) 272 _, err = m.AddPod(ctx, podname, "CPU") 273 s.NoError(err) 274 err = m.BatchCreate(ctx, map[string]string{fmt.Sprintf(nodeInfoKey, nodename): string(nodeBytes)}) 275 s.NoError(err) 276 err = m.BatchCreate(ctx, map[string]string{fmt.Sprintf(nodePodKey, podname, nodename): string(nodeBytes)}) 277 s.NoError(err) 278 s.NoError(m.AddWorkload(ctx, workload, nil)) 279 // WorkloadStatusStream 280 workload.StatusMeta = &types.StatusMeta{ 281 ID: id, 282 Running: true, 283 Appname: appname, 284 Entrypoint: entrypoint, 285 Nodename: nodename, 286 } 287 cctx, cancel := context.WithCancel(ctx) 288 ch := m.WorkloadStatusStream(cctx, appname, entrypoint, "", nil) 289 s.NoError(m.SetWorkloadStatus(ctx, workload.StatusMeta, 0)) 290 go func() { 291 time.Sleep(1 * time.Second) 292 cancel() 293 }() 294 for st := range ch { 295 s.False(st.Delete) 296 s.NotNil(st.Workload) 297 } 298 }