github.com/anuvu/tyk@v2.9.0-beta9-dl-apic+incompatible/cli/bundler/bundler_test.go (about) 1 package bundler 2 3 import ( 4 "archive/zip" 5 "bytes" 6 "encoding/json" 7 "io/ioutil" 8 "os" 9 "strings" 10 "testing" 11 12 "github.com/TykTechnologies/tyk/apidef" 13 14 kingpin "gopkg.in/alecthomas/kingpin.v2" 15 ) 16 17 var ( 18 testApp *kingpin.Application 19 20 standardManifest = &apidef.BundleManifest{ 21 FileList: []string{}, 22 CustomMiddleware: apidef.MiddlewareSection{ 23 Pre: []apidef.MiddlewareDefinition{ 24 { 25 Name: "MyPreHook", 26 }, 27 }, 28 Driver: "python", 29 }, 30 } 31 ) 32 33 func init() { 34 testApp = kingpin.New("tyk-cli", "") 35 AddTo(testApp) 36 37 // Setup default values: 38 bundlePath := defaultBundlePath 39 bundler.bundlePath = &bundlePath 40 manifestPath := defaultManifestPath 41 bundler.manifestPath = &manifestPath 42 } 43 44 func writeManifestFile(t testing.TB, manifest interface{}, filename string) *string { 45 var data []byte 46 var err error 47 switch manifest.(type) { 48 case *apidef.BundleManifest: 49 data, err = json.Marshal(&manifest) 50 if err != nil { 51 t.Fatalf("Couldn't marshal manifest file: %s", err.Error()) 52 } 53 case string: 54 manifestString := manifest.(string) 55 data = []byte(manifestString) 56 } 57 ioutil.WriteFile(filename, data, 0600) 58 if err != nil { 59 t.Fatalf("Couldn't write manifest file: %s", err.Error()) 60 } 61 return &filename 62 } 63 64 func TestCommands(t *testing.T) { 65 defer os.Remove(defaultManifestPath) 66 writeManifestFile(t, standardManifest, defaultManifestPath) 67 _, err := testApp.Parse([]string{"bundle", "build", "-y"}) 68 if err != nil { 69 t.Fatalf("Command not found") 70 } 71 } 72 func TestBuild(t *testing.T) { 73 defer os.Remove(defaultManifestPath) 74 75 // Test for common errors first: 76 t.Run("Bundle errors", func(t *testing.T) { 77 ctx := &kingpin.ParseContext{} 78 err := bundler.Build(ctx) 79 if err != errManifestLoad { 80 t.Fatalf("Expected manifest load error, got: %s", err.Error()) 81 } 82 filename := writeManifestFile(t, "{", defaultManifestPath) 83 bundler.manifestPath = filename 84 err = bundler.Build(ctx) 85 if !strings.Contains(err.Error(), "unexpected end of JSON input") { 86 t.Fatalf("Expected JSON error, got: %s", err.Error()) 87 } 88 filename = writeManifestFile(t, &apidef.BundleManifest{ 89 FileList: []string{}, 90 CustomMiddleware: apidef.MiddlewareSection{ 91 Pre: []apidef.MiddlewareDefinition{ 92 { 93 Name: "MyPreHook", 94 }, 95 }, 96 }, 97 }, defaultManifestPath) 98 bundler.manifestPath = filename 99 err = bundler.Build(ctx) 100 if err != errNoDriver { 101 t.Fatal("Expected no driver error") 102 } 103 filename = writeManifestFile(t, &apidef.BundleManifest{ 104 FileList: []string{}, 105 CustomMiddleware: apidef.MiddlewareSection{}, 106 }, defaultManifestPath) 107 bundler.manifestPath = filename 108 err = bundler.Build(ctx) 109 if err != errNoHooks { 110 t.Fatal("Expected no hooks error") 111 } 112 filename = writeManifestFile(t, &apidef.BundleManifest{ 113 FileList: []string{ 114 "middleware.py", 115 }, 116 CustomMiddleware: apidef.MiddlewareSection{ 117 Pre: []apidef.MiddlewareDefinition{ 118 { 119 Name: "MyPreHook", 120 }, 121 }, 122 Driver: "python", 123 }, 124 }, defaultManifestPath) 125 bundler.manifestPath = filename 126 err = bundler.Build(ctx) 127 if !strings.Contains(err.Error(), "nonexistent") { 128 t.Fatalf("Expected nonexistent file error, got %s", err.Error()) 129 } 130 }) 131 132 // Build a simple bundle: 133 t.Run("Simple bundle build", func(t *testing.T) { 134 ctx := &kingpin.ParseContext{} 135 err := ioutil.WriteFile("middleware.py", []byte(""), 0600) 136 if err != nil { 137 t.Fatalf("Couldn't write middleware.py: %s", err.Error()) 138 } 139 defer os.Remove("middleware.py") 140 filename := writeManifestFile(t, &apidef.BundleManifest{ 141 FileList: []string{ 142 "middleware.py", 143 }, 144 CustomMiddleware: apidef.MiddlewareSection{ 145 Pre: []apidef.MiddlewareDefinition{ 146 { 147 Name: "MyPreHook", 148 }, 149 }, 150 Driver: "python", 151 }, 152 }, defaultManifestPath) 153 bundler.manifestPath = filename 154 skipSigning := true 155 bundler.skipSigning = &skipSigning 156 err = bundler.Build(ctx) 157 zipFile, err := zip.OpenReader("bundle.zip") 158 if err != nil { 159 t.Fatalf("Couldn't initialize ZIP reader: %s\n", err.Error()) 160 } 161 defer func() { 162 zipFile.Close() 163 os.Remove("bundle.zip") 164 }() 165 if len(zipFile.File) != 2 { 166 t.Fatal("Number of bundled files doesn't match") 167 } 168 files := make(map[string][]byte) 169 for _, f := range zipFile.File { 170 reader, err := f.Open() 171 defer reader.Close() 172 if err != nil { 173 t.Fatalf("Couldn't read from ZIP file: %s", err.Error()) 174 } 175 if f.Name != defaultManifestPath && f.Name != "middleware.py" { 176 t.Fatalf("Unexpected file: %s", f.Name) 177 } 178 var buf bytes.Buffer 179 _, err = buf.ReadFrom(reader) 180 if err != nil { 181 t.Fatalf("Couldn't read from ZIP file: %s", err.Error()) 182 } 183 files[defaultManifestPath] = buf.Bytes() 184 } 185 manifestData, ok := files[defaultManifestPath] 186 if !ok { 187 t.Fatalf("Couldn't found manifest data: %s", err.Error()) 188 } 189 var manifest apidef.BundleManifest 190 err = json.Unmarshal(manifestData, &manifest) 191 if err != nil { 192 t.Fatalf("Couldn't decode manifest data: %s", err.Error()) 193 } 194 if manifest.Checksum != "d41d8cd98f00b204e9800998ecf8427e" { 195 t.Fatalf("Bundle checksum doesn't match") 196 } 197 preHooks := manifest.CustomMiddleware.Pre 198 if len(preHooks) != 1 { 199 t.Fatalf("Bundle hooks doesn't match, got %d, expected 1", len(preHooks)) 200 } 201 fileList := manifest.FileList 202 if len(fileList) != 1 { 203 t.Fatalf("Bundle file list doesn't match, got %d, expected 1", len(fileList)) 204 } 205 if fileList[0] != "middleware.py" { 206 t.Fatal("Bundle file 'middleware.py' wasn't found") 207 } 208 if manifest.CustomMiddleware.Driver != apidef.PythonDriver { 209 t.Fatalf("Bundle driver doesn't match, got %s, expected %s", manifest.CustomMiddleware.Driver, apidef.PythonDriver) 210 } 211 }) 212 }