github.com/Oppodelldog/droxy@v0.8.0/proxyfile/proxyfilecreator_test.go (about) 1 package proxyfile 2 3 import ( 4 "io/ioutil" 5 "log" 6 "os" 7 "path/filepath" 8 "reflect" 9 "runtime" 10 "testing" 11 12 "github.com/Oppodelldog/droxy/logger" 13 14 "github.com/Oppodelldog/droxy/config" 15 16 "github.com/stretchr/testify/assert" 17 ) 18 19 const testFolder = "/tmp/droxy/createProxyFilesTest" 20 21 func TestCreator_New(t *testing.T) { 22 fileCreatorMock := &mockFileCreationStrategy{} 23 configLoaderMock := &configLoaderMock{} 24 creator := New(fileCreatorMock, configLoaderMock) 25 26 assert.IsType(t, Creator{}, creator) 27 assert.Exactly(t, fileCreatorMock, creator.creationStrategy) 28 assert.Exactly(t, configLoaderMock, creator.configLoader) 29 30 if reflect.ValueOf(creator.getExecutableFilePathFunc).Pointer() != reflect.ValueOf(getExecutableFilePath).Pointer() { 31 t.Fatal("expected 'getExecutableFilePath' to be configured as getExecutableFilePathFunc, but was not") 32 } 33 } 34 35 func getTestConfig() config.Configuration { 36 commandNameStub := "some-command-name" 37 38 return config.Configuration{ 39 Command: []config.CommandDefinition{ 40 {Name: &commandNameStub}, 41 }, 42 } 43 } 44 45 func getTestConfigWithEmptyCommand() config.Configuration { 46 return config.Configuration{Command: []config.CommandDefinition{{}}} 47 } 48 49 func TestCreator_CreateProxyFiles(t *testing.T) { 50 defer prepareTest(t)() 51 52 commandBinaryFilePathStub := "/tmp/droxy" 53 54 fileCreatorMock := &mockFileCreationStrategy{} 55 configLoaderMock := &configLoaderMock{stubbedConfig: getTestConfig()} 56 creator := &Creator{ 57 creationStrategy: fileCreatorMock, 58 configLoader: configLoaderMock, 59 getExecutableFilePathFunc: func() (string, error) { return commandBinaryFilePathStub, nil }, 60 } 61 62 err := creator.CreateProxyFiles(false) 63 if err != nil { 64 t.Fatalf("Did not expect CreateProxyFiles to return an error, but got: %v", err) 65 } 66 67 expectedCommandFilename := ensureOsSpecificBinaryFilename(*configLoaderMock.stubbedConfig.Command[0].Name) 68 69 assert.Equal(t, 1, fileCreatorMock.calls) 70 assert.Equal(t, commandBinaryFilePathStub, fileCreatorMock.parmCommandBinaryFilePath) 71 assert.Equal(t, expectedCommandFilename, fileCreatorMock.parmCommandNameFileName) 72 } 73 74 func TestCreator_CreateProxyFiles_commandHasNoName_noFileWillBeCreated(t *testing.T) { 75 defer prepareTest(t)() 76 77 fileCreatorMock := &mockFileCreationStrategy{} 78 configLoaderMock := &configLoaderMock{stubbedConfig: getTestConfigWithEmptyCommand()} 79 creator := &Creator{ 80 creationStrategy: fileCreatorMock, 81 configLoader: configLoaderMock, 82 getExecutableFilePathFunc: func() (string, error) { return "", nil }, 83 } 84 85 err := creator.CreateProxyFiles(false) 86 if err != nil { 87 t.Fatalf("Did not expect CreateProxyFiles to return an error, but got: %v", err) 88 } 89 90 assert.Equal(t, 0, fileCreatorMock.calls) 91 } 92 93 func TestCreator_CreateProxyFiles_commandIsTemplate_noFileWillBeCreated(t *testing.T) { 94 defer prepareTest(t)() 95 96 fileCreatorMock := &mockFileCreationStrategy{} 97 testConfig := getTestConfig() 98 isTemplate := true 99 testConfig.Command[0].IsTemplate = &isTemplate 100 configLoaderMock := &configLoaderMock{stubbedConfig: testConfig} 101 creator := &Creator{ 102 creationStrategy: fileCreatorMock, 103 configLoader: configLoaderMock, 104 getExecutableFilePathFunc: func() (string, error) { return "", nil }, 105 } 106 107 err := creator.CreateProxyFiles(false) 108 if err != nil { 109 t.Fatalf("Did not expect CreateProxyFiles to return an error, but got: %v", err) 110 } 111 112 assert.Equal(t, 0, fileCreatorMock.calls) 113 } 114 115 func TestCreator_CreateProxyFiles_fileAlreadyExistsAndCreationIsNotForced_existingFileWillNotBeReplaced(t *testing.T) { 116 defer prepareTest(t)() 117 118 logger.SetOutput(ioutil.Discard) 119 120 fileCreatorMock := &mockFileCreationStrategy{} 121 configLoaderMock := &configLoaderMock{stubbedConfig: getTestConfig()} 122 creator := &Creator{ 123 creationStrategy: fileCreatorMock, 124 configLoader: configLoaderMock, 125 getExecutableFilePathFunc: func() (string, error) { return "", nil }, 126 } 127 128 commandNameStub := *configLoaderMock.stubbedConfig.Command[0].Name 129 fileThatShouldNotBeDeleted := commandNameStub 130 131 err := ioutil.WriteFile(fileThatShouldNotBeDeleted, []byte("TEST"), writePerm) 132 if err != nil { 133 t.Fatalf("Did not expect ioutil.WriteFile to return an error, but got: %v", err) 134 } 135 136 err = creator.CreateProxyFiles(false) 137 if err != nil { 138 t.Fatalf("Did not expect CreateProxyFiles to return an error, but got: %v", err) 139 } 140 141 _, errStat := os.Stat(fileThatShouldNotBeDeleted) 142 assert.Nil(t, errStat, "Expect no error, since file should not have been deleted") 143 144 err = os.Remove(fileThatShouldNotBeDeleted) 145 if err != nil { 146 t.Fatalf("Did not expect os.Remove to return an error, but got: %v", err) 147 } 148 } 149 150 func TestCreator_CreateProxyFiles_fileAlreadyExistsAsDirectoryAndCreationIsForced_folderWillNotBeDeleted(t *testing.T) { 151 defer prepareTest(t)() 152 153 logger.SetOutput(ioutil.Discard) 154 155 fileCreatorMock := &mockFileCreationStrategy{} 156 configLoaderMock := &configLoaderMock{stubbedConfig: getTestConfig()} 157 creator := &Creator{ 158 creationStrategy: fileCreatorMock, 159 configLoader: configLoaderMock, 160 getExecutableFilePathFunc: func() (string, error) { return "", nil }, 161 } 162 163 commandNameStub := *configLoaderMock.stubbedConfig.Command[0].Name 164 folderThatShallNotBeDeleted := commandNameStub 165 166 err := os.MkdirAll(folderThatShallNotBeDeleted, 0666) 167 if err != nil { 168 t.Fatalf("Did not expect os.MkdirAll to return an error, but got: %v", err) 169 } 170 171 err = creator.CreateProxyFiles(true) 172 if err != nil { 173 t.Fatalf("Did not expect CreateProxyFiles to return an error, but got: %v", err) 174 } 175 176 _, err = os.Stat(folderThatShallNotBeDeleted) 177 assert.NoError(t, err, "Expect no error, since folder should not be deleted, but got: %v", err) 178 } 179 180 func TestCreator_CreateProxyFiles_fileAlreadyExistsAndCreationIsForced_existingFileWillBeReplaced(t *testing.T) { 181 defer prepareTest(t)() 182 183 logger.SetOutput(ioutil.Discard) 184 185 fileCreatorMock := &mockFileCreationStrategy{} 186 configLoaderMock := &configLoaderMock{stubbedConfig: getTestConfig()} 187 creator := &Creator{ 188 creationStrategy: fileCreatorMock, 189 configLoader: configLoaderMock, 190 getExecutableFilePathFunc: func() (string, error) { return "", nil }, 191 } 192 193 commandNameStub := *configLoaderMock.stubbedConfig.Command[0].Name 194 fileThatShouldBeDeleted := ensureOsSpecificBinaryFilename(commandNameStub) 195 196 err := ioutil.WriteFile(fileThatShouldBeDeleted, []byte("TEST"), writePerm) 197 if err != nil { 198 t.Fatalf("Did not expect ioutil.WriteFile to return an error, but got: %v", err) 199 } 200 201 err = creator.CreateProxyFiles(true) 202 if err != nil { 203 t.Fatalf("Did not expect CreateProxyFiles to return an error, but got: %v", err) 204 } 205 206 _, err = os.Stat(fileThatShouldBeDeleted) 207 assert.Error(t, err, "Expect error, since file should be deleted") 208 } 209 210 func ensureOsSpecificBinaryFilename(filePath string) string { 211 if runtime.GOOS == "windows" { 212 filePath += ".exe" 213 } 214 215 return filePath 216 } 217 218 func prepareTest(t *testing.T) func() { 219 logger.SetOutput(ioutil.Discard) 220 221 err := os.RemoveAll(testFolder) 222 if err != nil { 223 t.Fatalf("Did not expect os.RemoveAll to return an error, but got: %v", err) 224 } 225 226 err = os.MkdirAll(testFolder, 0776) 227 if err != nil { 228 t.Fatalf("Did not expect os.MkdirAll to return an error, but got: %v", err) 229 } 230 231 originalDir, err := filepath.Abs(filepath.Dir(os.Args[0])) 232 if err != nil { 233 log.Fatal(err) 234 } 235 236 err = os.Chdir(testFolder) 237 if err != nil { 238 t.Fatalf("Did not expect os.Chdir to return an error, but got: %v", err) 239 } 240 241 return func() { 242 err = os.Chdir(originalDir) 243 if err != nil { 244 t.Fatalf("Did not expect os.Chdir to return an error when switching back to original dir, but got: %v", err) 245 } 246 247 err := os.RemoveAll(testFolder) 248 if err != nil { 249 t.Fatalf("Did not expect os.RemoveAll to return an error, but got: %v", err) 250 } 251 } 252 }