|
| 1 | +package bootkube |
| 2 | + |
| 3 | +import ( |
| 4 | + "io/ioutil" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "reflect" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/kubernetes-incubator/bootkube/pkg/asset" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + secrets = []string{"secret-1.yaml", "secret-2.yaml", "secret-3.yaml"} |
| 15 | + manifests = []string{"pod-1.yaml", "pod-2.yaml"} |
| 16 | +) |
| 17 | + |
| 18 | +func setUp(t *testing.T) (assetDir, podManifestPath string) { |
| 19 | + // Create source directories. |
| 20 | + var err error |
| 21 | + assetDir, err = ioutil.TempDir("", "assets") |
| 22 | + if err != nil { |
| 23 | + t.Fatal(err) |
| 24 | + } |
| 25 | + podManifestPath, err = ioutil.TempDir("", "manifests") |
| 26 | + if err != nil { |
| 27 | + t.Fatal(err) |
| 28 | + } |
| 29 | + asset.BootstrapSecretsDir, err = ioutil.TempDir("", "bootstrap-secrets") |
| 30 | + if err != nil { |
| 31 | + t.Fatal(err) |
| 32 | + } |
| 33 | + |
| 34 | + // Create assets. |
| 35 | + if err := os.Mkdir(filepath.Join(assetDir, asset.AssetPathSecrets), os.FileMode(0755)); err != nil { |
| 36 | + t.Fatal(err) |
| 37 | + } |
| 38 | + for _, secret := range secrets { |
| 39 | + if err := ioutil.WriteFile(filepath.Join(assetDir, asset.AssetPathSecrets, secret), []byte("secret data"), os.FileMode(0644)); err != nil { |
| 40 | + t.Fatal(err) |
| 41 | + } |
| 42 | + } |
| 43 | + if err := os.Mkdir(filepath.Join(assetDir, asset.AssetPathBootstrapManifests), os.FileMode(0755)); err != nil { |
| 44 | + t.Fatal(err) |
| 45 | + } |
| 46 | + for _, manifest := range manifests { |
| 47 | + if err := ioutil.WriteFile(filepath.Join(assetDir, asset.AssetPathBootstrapManifests, manifest), []byte("manifest data"), os.FileMode(0644)); err != nil { |
| 48 | + t.Fatal(err) |
| 49 | + } |
| 50 | + } |
| 51 | + return |
| 52 | +} |
| 53 | + |
| 54 | +func tearDown(assetDir, podManifestPath string, t *testing.T) { |
| 55 | + if err := os.RemoveAll(assetDir); err != nil { |
| 56 | + t.Fatal(err) |
| 57 | + } |
| 58 | + if err := os.RemoveAll(podManifestPath); err != nil { |
| 59 | + t.Fatal(err) |
| 60 | + } |
| 61 | + if err := os.RemoveAll(asset.BootstrapSecretsDir); err != nil { |
| 62 | + t.Fatal(err) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func TestBootstrapControlPlane(t *testing.T) { |
| 67 | + assetDir, podManifestPath := setUp(t) |
| 68 | + defer tearDown(assetDir, podManifestPath, t) |
| 69 | + |
| 70 | + // Create and start bootstrap control plane. |
| 71 | + bcp := NewBootstrapControlPlane(assetDir, podManifestPath) |
| 72 | + if err := bcp.Start(); err != nil { |
| 73 | + t.Errorf("bcp.Start() = %v, want: nil", err) |
| 74 | + } |
| 75 | + |
| 76 | + // Make sure assets were copied. |
| 77 | + for _, secret := range secrets { |
| 78 | + if _, err := os.Stat(filepath.Join(asset.BootstrapSecretsDir, secret)); os.IsNotExist(err) { |
| 79 | + t.Errorf("bcp.Start() failed to copy secret: %v", secret) |
| 80 | + } |
| 81 | + } |
| 82 | + for _, manifest := range manifests { |
| 83 | + if _, err := os.Stat(filepath.Join(podManifestPath, manifest)); os.IsNotExist(err) { |
| 84 | + t.Errorf("bcp.Start() failed to copy manifest: %v", manifest) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Tear down control plane. |
| 89 | + if err := bcp.Teardown(); err != nil { |
| 90 | + t.Errorf("bcp.Teardown() = %v, want: nil", err) |
| 91 | + } |
| 92 | + |
| 93 | + // Make sure directories were properly cleaned up. |
| 94 | + if fi, err := os.Stat(asset.BootstrapSecretsDir); fi != nil || !os.IsNotExist(err) { |
| 95 | + t.Error("bcp.Teardown() failed to delete secrets directory") |
| 96 | + } |
| 97 | + for _, manifest := range manifests { |
| 98 | + if fi, err := os.Stat(filepath.Join(podManifestPath, manifest)); fi != nil || !os.IsNotExist(err) { |
| 99 | + t.Errorf("bcp.Teardown() failed to delete manifest: %v", manifest) |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func TestBootstrapControlPlaneNoOverwrite(t *testing.T) { |
| 105 | + assetDir, podManifestPath := setUp(t) |
| 106 | + defer tearDown(assetDir, podManifestPath, t) |
| 107 | + existingManifest := manifests[1] |
| 108 | + existingData := []byte("existing data") |
| 109 | + |
| 110 | + // Create a manifest in the destination already. |
| 111 | + if err := ioutil.WriteFile(filepath.Join(podManifestPath, existingManifest), existingData, os.FileMode(0644)); err != nil { |
| 112 | + t.Fatal(err) |
| 113 | + } |
| 114 | + |
| 115 | + // Create and start bootstrap control plane. |
| 116 | + bcp := NewBootstrapControlPlane(assetDir, podManifestPath) |
| 117 | + if err := bcp.Start(); err == nil { |
| 118 | + t.Errorf("bcp.Start() = %v, want: non-nil", err) |
| 119 | + } |
| 120 | + |
| 121 | + // Make sure assets were copied. |
| 122 | + for _, secret := range secrets { |
| 123 | + if _, err := os.Stat(filepath.Join(asset.BootstrapSecretsDir, secret)); os.IsNotExist(err) { |
| 124 | + t.Errorf("bcp.Start() failed to copy secret: %v", secret) |
| 125 | + } |
| 126 | + } |
| 127 | + for _, manifest := range manifests { |
| 128 | + if _, err := os.Stat(filepath.Join(podManifestPath, manifest)); os.IsNotExist(err) { |
| 129 | + t.Errorf("bcp.Start() failed to copy manifest: %v", manifest) |
| 130 | + } |
| 131 | + if manifest == existingManifest { |
| 132 | + data, err := ioutil.ReadFile(filepath.Join(podManifestPath, manifest)) |
| 133 | + if err != nil { |
| 134 | + t.Fatal(err) |
| 135 | + } |
| 136 | + if !reflect.DeepEqual(data, existingData) { |
| 137 | + t.Errorf("existing manifest %v was overwritten, got: %s, want: %s", existingManifest, data, existingData) |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + // Tear down control plane. |
| 143 | + if err := bcp.Teardown(); err != nil { |
| 144 | + t.Errorf("bcp.Start() = %v, want: nil", err) |
| 145 | + } |
| 146 | + |
| 147 | + // Make sure directories were properly cleaned up. |
| 148 | + if fi, err := os.Stat(asset.BootstrapSecretsDir); fi != nil || !os.IsNotExist(err) { |
| 149 | + t.Error("bcp.Teardown() failed to delete secrets directory") |
| 150 | + } |
| 151 | + for _, manifest := range manifests { |
| 152 | + if manifest == existingManifest { |
| 153 | + continue // The manifest previously existed -- do not delete. |
| 154 | + } |
| 155 | + if fi, err := os.Stat(filepath.Join(podManifestPath, manifest)); fi != nil || !os.IsNotExist(err) { |
| 156 | + t.Errorf("bcp.Teardown() failed to delete manifest: %v", manifest) |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments