|
| 1 | +package engine |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +func TestEngineCanaries(t *testing.T) { |
| 10 | + etcdAddr := "http://127.0.0.1:2379" |
| 11 | + ConfigPortsManager(etcdAddr) |
| 12 | + c, store, err := initClusterAndStore() |
| 13 | + if err != nil { |
| 14 | + t.Fatalf("Cannot create the cluster and storage, %s", err) |
| 15 | + } |
| 16 | + |
| 17 | + engine, err := New(c, store) |
| 18 | + if err != nil { |
| 19 | + t.Fatalf("Cannot create the orc engine, %s", err) |
| 20 | + } |
| 21 | + |
| 22 | + namespace := "hello" |
| 23 | + name := "hello.canary.web" |
| 24 | + pgSpec := createPodGroupSpec(namespace, name, 1) |
| 25 | + data := map[string]interface{}{"suffix": 1, "upstream": "beta1"} |
| 26 | + strategies := []IStrategy{&DefaultStrategy{"uidsuffix", []interface{}{data}}} |
| 27 | + canary := &Canary{strategies} |
| 28 | + canarySpec := CanaryPodsWithSpec{pgSpec, canary} |
| 29 | + if err := engine.NewCanary(canarySpec); err != nil { |
| 30 | + t.Fatalf("Should not return error, %s", err) |
| 31 | + } |
| 32 | + if err := engine.NewCanary(canarySpec); err == nil { |
| 33 | + t.Errorf("Should return exists error, but we got no problem") |
| 34 | + } |
| 35 | + |
| 36 | + time.Sleep(20 * time.Second) |
| 37 | + if pg, ok := engine.InspectPodGroup(name); !ok { |
| 38 | + t.Errorf("We should have the pod group, but we don't get it") |
| 39 | + } else if pg.State != RunStateSuccess { |
| 40 | + t.Errorf("We should have the pod deployed and running") |
| 41 | + } |
| 42 | + |
| 43 | + engine.RescheduleInstance(name, 3) |
| 44 | + time.Sleep(20 * time.Second) |
| 45 | + if pg, ok := engine.InspectPodGroup(name); !ok { |
| 46 | + t.Errorf("We should have the pod group, but we don't get it") |
| 47 | + } else if len(pg.Pods) != 3 { |
| 48 | + t.Errorf("We should have 3 instance of the pods") |
| 49 | + } |
| 50 | + |
| 51 | + engine.RescheduleInstance(name, 1) |
| 52 | + time.Sleep(30 * time.Second) |
| 53 | + if pg, ok := engine.InspectPodGroup(name); !ok { |
| 54 | + t.Errorf("We should have the pod group, but we don't get it") |
| 55 | + } else if len(pg.Pods) != 1 { |
| 56 | + bytes, err := json.Marshal(pg.Pods) |
| 57 | + pods := "" |
| 58 | + if err == nil { |
| 59 | + pods = string(bytes) |
| 60 | + } |
| 61 | + t.Errorf("We should have 1 instance of the pods : %v", pods) |
| 62 | + } |
| 63 | + |
| 64 | + podSpec := createPodSpec(namespace, name) |
| 65 | + podSpec.Containers[0].MemoryLimit = 24 * 1024 * 1024 |
| 66 | + engine.RescheduleSpec(name, podSpec) |
| 67 | + time.Sleep(40 * time.Second) |
| 68 | + if pg, ok := engine.InspectPodGroup(name); !ok { |
| 69 | + t.Errorf("We should have the pod group, but we don't get it") |
| 70 | + } else if pg.Spec.Version != 1 { |
| 71 | + t.Errorf("We should have version 1 of the pods") |
| 72 | + } |
| 73 | + |
| 74 | + if nCanary := engine.FetchCanaryInfos(name); nCanary == nil { |
| 75 | + t.Fatalf("Should return canary!") |
| 76 | + } else { |
| 77 | + if !nCanary.Equal(canary) { |
| 78 | + t.Fatalf("Canary info should not changed") |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if err := engine.RemovePodGroup(name); err != nil { |
| 83 | + t.Errorf("We should be able to remove the pod group, %s", err) |
| 84 | + } else if err := engine.NewPodGroup(pgSpec); err == nil { |
| 85 | + t.Errorf("We should not be able to deploy pod group again in short time we remove it") |
| 86 | + } |
| 87 | + time.Sleep(20 * time.Second) |
| 88 | + |
| 89 | + if nCanary := engine.FetchCanaryInfos(name); nCanary != nil { |
| 90 | + t.Fatalf("Should not return nCanary") |
| 91 | + } |
| 92 | +} |
0 commit comments