|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/docker/docker/api/types/volume" |
| 8 | + "github.com/docker/docker/client" |
| 9 | +) |
| 10 | + |
| 11 | +func setupMgr(t *testing.T) *ContainerMgr { |
| 12 | + cli, err := client.NewClientWithOpts(client.FromEnv) |
| 13 | + if err != nil { |
| 14 | + t.Fatalf("Failed to create Docker client: %v", err) |
| 15 | + } |
| 16 | + return NewContainerMgr(cli, 10, 100) |
| 17 | +} |
| 18 | + |
| 19 | +// T1: create a volume, check exists, delete, check not exists |
| 20 | +func TestCreateDeleteVolume(t *testing.T) { |
| 21 | + mgr := setupMgr(t) |
| 22 | + volName := "test_volume_t1" |
| 23 | + mgr.createVolume(volName) |
| 24 | + vols, _ := mgr.cli.VolumeList(mgr.ctx, volume.ListOptions{}) |
| 25 | + found := false |
| 26 | + for _, v := range vols.Volumes { |
| 27 | + if v.Name == volName { |
| 28 | + found = true |
| 29 | + break |
| 30 | + } |
| 31 | + } |
| 32 | + if !found { |
| 33 | + t.Errorf("Volume %s not found after creation", volName) |
| 34 | + } |
| 35 | + mgr.removeVolume(volName, true) |
| 36 | + vols, _ = mgr.cli.VolumeList(mgr.ctx, volume.ListOptions{}) |
| 37 | + for _, v := range vols.Volumes { |
| 38 | + if v.Name == volName { |
| 39 | + t.Errorf("Volume %s still exists after deletion", volName) |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// T3: create a volume with same name twice (should not fail) |
| 45 | +func TestCreateVolumeTwice(t *testing.T) { |
| 46 | + mgr := setupMgr(t) |
| 47 | + volName := "test_volume_t3" |
| 48 | + mgr.createVolume(volName) |
| 49 | + defer mgr.removeVolume(volName, true) |
| 50 | + mgr.createVolume(volName) // Should not fail |
| 51 | +} |
| 52 | + |
| 53 | +// T4: remove volume that doesn't exist (should fail or panic) |
| 54 | +func TestRemoveNonexistentVolume(t *testing.T) { |
| 55 | + mgr := setupMgr(t) |
| 56 | + err := mgr.removeVolume("nonexistent_volume_t4", true) |
| 57 | + if err == nil { |
| 58 | + t.Errorf("Expected error when removing nonexistent volume, but no error") |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// T5: remove volume in use (should fail or panic) |
| 63 | +func TestRemoveVolumeInUse(t *testing.T) { |
| 64 | + mgr := setupMgr(t) |
| 65 | + volName := "test_volume_t5" |
| 66 | + mgr.createVolume(volName) |
| 67 | + containerID, err := mgr.runContainerCuda(volName) |
| 68 | + if err != nil { |
| 69 | + fmt.Errorf("Failed to start container: %v", err.Error()) |
| 70 | + } |
| 71 | + defer func() { |
| 72 | + mgr.stopContainer(containerID) |
| 73 | + mgr.removeContainer(containerID) |
| 74 | + mgr.removeVolume(volName, true) |
| 75 | + }() |
| 76 | + err = mgr.removeVolume(volName, true) // why didn't this panic? |
| 77 | + if err == nil { |
| 78 | + t.Errorf("Expected error when removing nonexistent volume, but no error") |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// T6: attach a volume that does not exist (should fail or panic) |
| 83 | +func TestAttachNonexistentVolume(t *testing.T) { |
| 84 | + mgr := setupMgr(t) |
| 85 | + |
| 86 | + id, err := mgr.runContainerCuda("nonexistent_volume_t6") |
| 87 | + if id != "" && err != nil { |
| 88 | + t.Errorf("Expected error when removing nonexistent volume, but no error") |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// T7: two containers attach to the same volume (should succeed in Docker, but test for your policy) |
| 93 | +func TestTwoContainersSameVolume(t *testing.T) { |
| 94 | + mgr := setupMgr(t) |
| 95 | + volName := "test_volume_t7" |
| 96 | + mgr.createVolume(volName) |
| 97 | + id1, err := mgr.runContainerCuda(volName) |
| 98 | + if err != nil { |
| 99 | + fmt.Errorf("Failed to start container: %v", err.Error()) |
| 100 | + } |
| 101 | + id2, err := mgr.runContainerCuda(volName) |
| 102 | + if err != nil { |
| 103 | + fmt.Errorf("Failed to start container: %v", err.Error()) |
| 104 | + } |
| 105 | + mgr.stopContainer(id1) |
| 106 | + mgr.removeContainer(id1) |
| 107 | + mgr.stopContainer(id2) |
| 108 | + mgr.removeContainer(id2) |
| 109 | + mgr.removeVolume(volName, true) |
| 110 | +} |
| 111 | + |
| 112 | +// T8: two containers try to attach to the same volume at the same time (should succeed in Docker) |
| 113 | +func TestTwoContainersSameVolumeConcurrent(t *testing.T) { |
| 114 | + mgr := setupMgr(t) |
| 115 | + volName := "test_volume_t8" |
| 116 | + mgr.createVolume(volName) |
| 117 | + id1, err := mgr.runContainerCuda(volName) |
| 118 | + if err != nil { |
| 119 | + fmt.Errorf("Failed to start container: %v", err.Error()) |
| 120 | + } |
| 121 | + id2, err2 := mgr.runContainerCuda(volName) |
| 122 | + if err2 != nil { |
| 123 | + fmt.Errorf("Failed to start container: %v", err2.Error()) |
| 124 | + } |
| 125 | + mgr.stopContainer(id1) |
| 126 | + mgr.removeContainer(id1) |
| 127 | + mgr.stopContainer(id2) |
| 128 | + mgr.removeContainer(id2) |
| 129 | + mgr.removeVolume(volName, true) |
| 130 | +} |
| 131 | + |
| 132 | +// T9: set a limit of 100 volumes (should fail on 101st if you enforce a limit) |
| 133 | +func TestVolumeLimit(t *testing.T) { |
| 134 | + mgr := setupMgr(t) |
| 135 | + limit := 100 |
| 136 | + created := []string{} |
| 137 | + for i := 0; i < limit; i++ { |
| 138 | + name := "test_volume_t9_" + fmt.Sprint(i) |
| 139 | + mgr.createVolume(name) |
| 140 | + created = append(created, name) |
| 141 | + } |
| 142 | + name := "test_volume_fail" |
| 143 | + _, err := mgr.createVolume(name) |
| 144 | + if err == nil { |
| 145 | + fmt.Errorf("Volume limit not enforced") |
| 146 | + } |
| 147 | + |
| 148 | + defer func() { |
| 149 | + for _, name := range created { |
| 150 | + mgr.removeVolume(name, true) |
| 151 | + } |
| 152 | + }() |
| 153 | + // why didn't you clean up the volumes |
| 154 | + // Try to create one more if you enforce a limit |
| 155 | + // If not enforced, this will succeed |
| 156 | +} |
| 157 | + |
| 158 | +// T10: set a limit of 10 containers (should fail on 11th if you enforce a limit) |
| 159 | +func TestContainerLimit(t *testing.T) { |
| 160 | + mgr := setupMgr(t) |
| 161 | + volName := "test_volume_t10" |
| 162 | + mgr.createVolume(volName) |
| 163 | + ids := []string{} |
| 164 | + limit := 10 |
| 165 | + for i := 0; i < limit; i++ { |
| 166 | + id, err := mgr.runContainerCuda(volName) |
| 167 | + if err != nil { |
| 168 | + fmt.Errorf("Failed to start container: %v", err.Error()) |
| 169 | + } |
| 170 | + ids = append(ids, id) |
| 171 | + } |
| 172 | + _, err := mgr.runContainerCuda(volName) |
| 173 | + if err == nil { |
| 174 | + fmt.Errorf("Container limit not enforced") |
| 175 | + } |
| 176 | + defer func() { |
| 177 | + for _, id := range ids { |
| 178 | + mgr.stopContainer(id) |
| 179 | + mgr.removeContainer(id) |
| 180 | + } |
| 181 | + mgr.removeVolume(volName, true) // why didnt you clean up the containers? |
| 182 | + }() |
| 183 | + // Try to create one more if you enforce a limit |
| 184 | + // If not enforced, this will succeed |
| 185 | +} |
0 commit comments