github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/cmd/firefly_test.go (about) 1 // Copyright © 2021 Kaleido, Inc. 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 package cmd 18 19 import ( 20 "fmt" 21 "os" 22 "syscall" 23 "testing" 24 25 "github.com/kaleido-io/firefly/mocks/orchestratormocks" 26 "github.com/spf13/viper" 27 "github.com/stretchr/testify/assert" 28 "github.com/stretchr/testify/mock" 29 ) 30 31 const configDir = "../test/data/config" 32 33 func TestGetEngine(t *testing.T) { 34 assert.NotNil(t, getOrchestrator()) 35 } 36 37 func TestExecMissingConfig(t *testing.T) { 38 _utOrchestrator = &orchestratormocks.Orchestrator{} 39 defer func() { _utOrchestrator = nil }() 40 viper.Reset() 41 err := Execute() 42 assert.Regexp(t, "Not Found", err) 43 } 44 45 func TestShowConfig(t *testing.T) { 46 _utOrchestrator = &orchestratormocks.Orchestrator{} 47 defer func() { _utOrchestrator = nil }() 48 viper.Reset() 49 rootCmd.SetArgs([]string{"showconf"}) 50 defer rootCmd.SetArgs([]string{}) 51 err := rootCmd.Execute() 52 assert.NoError(t, err) 53 } 54 55 func TestExecEngineInitFail(t *testing.T) { 56 o := &orchestratormocks.Orchestrator{} 57 o.On("Init", mock.Anything).Return(fmt.Errorf("splutter")) 58 _utOrchestrator = o 59 defer func() { _utOrchestrator = nil }() 60 os.Chdir(configDir) 61 err := Execute() 62 assert.Regexp(t, "splutter", err) 63 } 64 65 func TestExecEngineStartFail(t *testing.T) { 66 o := &orchestratormocks.Orchestrator{} 67 o.On("Init", mock.Anything).Return(nil) 68 o.On("Start").Return(fmt.Errorf("bang")) 69 _utOrchestrator = o 70 defer func() { _utOrchestrator = nil }() 71 os.Chdir(configDir) 72 err := Execute() 73 assert.Regexp(t, "bang", err) 74 } 75 76 func TestExecOkExitSIGINT(t *testing.T) { 77 o := &orchestratormocks.Orchestrator{} 78 o.On("Init", mock.Anything).Return(nil) 79 o.On("Start").Return(nil) 80 o.On("WaitStop").Return() 81 _utOrchestrator = o 82 defer func() { _utOrchestrator = nil }() 83 84 os.Chdir(configDir) 85 go func() { 86 sigs <- syscall.SIGINT 87 }() 88 err := Execute() 89 assert.NoError(t, err) 90 }