github.com/Axway/agent-sdk@v1.1.101/pkg/cmd/service/service_test.go (about) 1 package service 2 3 import ( 4 "os" 5 "testing" 6 7 "github.com/Axway/agent-sdk/pkg/cmd/service/daemon" 8 "github.com/stretchr/testify/assert" 9 ) 10 11 const ( 12 UNEXPECTED_ERR = "Unexpected error returned" 13 ) 14 15 type mockDaemon struct { 16 installCalled bool 17 updateCalled bool 18 removeCalled bool 19 startCalled bool 20 stopCalled bool 21 statusCalled bool 22 logsCalled bool 23 runCalled bool 24 enableCalled bool 25 serviceNameCalled bool 26 } 27 28 func (m *mockDaemon) GetTemplate() string { return "" } 29 func (m *mockDaemon) SetTemplate(string) error { return nil } 30 func (m *mockDaemon) SetEnvFile(string) error { return nil } 31 func (m *mockDaemon) SetUser(string) error { return nil } 32 func (m *mockDaemon) SetGroup(string) error { return nil } 33 34 func (m *mockDaemon) Install(args ...string) (string, error) { 35 m.installCalled = true 36 return "", nil 37 } 38 39 func (m *mockDaemon) Update(args ...string) (string, error) { 40 m.updateCalled = true 41 return "", nil 42 } 43 44 func (m *mockDaemon) Remove() (string, error) { 45 m.removeCalled = true 46 return "", nil 47 } 48 49 func (m *mockDaemon) Start() (string, error) { 50 m.startCalled = true 51 return "", nil 52 } 53 54 func (m *mockDaemon) Stop() (string, error) { 55 m.stopCalled = true 56 return "", nil 57 } 58 59 func (m *mockDaemon) Status() (string, error) { 60 m.statusCalled = true 61 return "", nil 62 } 63 64 func (m *mockDaemon) Logs() (string, error) { 65 m.logsCalled = true 66 return "", nil 67 } 68 69 func (m *mockDaemon) Run(e daemon.Executable) (string, error) { 70 m.runCalled = true 71 return "", nil 72 } 73 74 func (m *mockDaemon) Enable() (string, error) { 75 m.enableCalled = true 76 return "", nil 77 } 78 79 func (m *mockDaemon) GetServiceName() string { 80 m.serviceNameCalled = true 81 return "" 82 } 83 84 func newMockAgentService() *AgentService { 85 return &AgentService{ 86 service: &mockDaemon{}, 87 Name: "disco-agent", 88 Description: "description", 89 Path: "/this/path", 90 PathArg: "--pathConfig", 91 User: "user", 92 Group: "group", 93 EnvFile: "./filename", 94 } 95 } 96 97 func TestGenServiceCmd(t *testing.T) { 98 cmd := GenServiceCmd("pathConfig") 99 100 assert.NotNil(t, cmd, "The generated command was nil") 101 assert.Contains(t, cmd.Short, "Manage the OS service") 102 assert.Contains(t, cmd.Long, "Manage the OS service") 103 assert.Contains(t, cmd.Long, argDescriptions["install"], "The install description was not included in the long description") 104 assert.Contains(t, cmd.Long, argDescriptions["update"], "The update description was not included in the long description") 105 assert.Contains(t, cmd.Long, argDescriptions["remove"], "The remove description was not included in the long description") 106 assert.Contains(t, cmd.Long, argDescriptions["start"], "The start description was not included in the long description") 107 assert.Contains(t, cmd.Long, argDescriptions["stop"], "The stop description was not included in the long description") 108 assert.Contains(t, cmd.Long, argDescriptions["logs"], "The status description was not included in the long description") 109 assert.Contains(t, cmd.Long, argDescriptions["status"], "The status description was not included in the long description") 110 assert.Contains(t, cmd.Long, argDescriptions["enable"], "The enable description was not included in the long description") 111 assert.Contains(t, cmd.Long, argDescriptions["name"], "The name description was not included in the long description") 112 } 113 114 func TestRunGenServiceCmd(t *testing.T) { 115 Name = "discovery-agent" 116 a := newMockAgentService() 117 globalAgentService = a 118 cmd := GenServiceCmd("pathConfig") 119 120 cmd.Flags().String("pathConfig", "", "") 121 cmd.SetArgs([]string{"install", "--pathConfig", "."}) 122 err := cmd.Execute() 123 124 assert.Nil(t, err, "Error expected to be returned from command Execute") 125 assert.True(t, a.service.(*mockDaemon).installCalled) 126 127 os.Remove("./filename") 128 } 129 130 func TestHandleService(t *testing.T) { 131 var a *AgentService 132 var err error 133 134 // Install 135 a = newMockAgentService() 136 err = a.HandleServiceFlag("install") 137 assert.Nil(t, err, UNEXPECTED_ERR) 138 assert.True(t, a.service.(*mockDaemon).installCalled) 139 140 // Update 141 a = newMockAgentService() 142 err = a.HandleServiceFlag("update") 143 assert.Nil(t, err, UNEXPECTED_ERR) 144 assert.True(t, a.service.(*mockDaemon).updateCalled) 145 146 // Remove 147 a = newMockAgentService() 148 err = a.HandleServiceFlag("remove") 149 assert.Nil(t, err, UNEXPECTED_ERR) 150 assert.True(t, a.service.(*mockDaemon).removeCalled) 151 152 // Start 153 a = newMockAgentService() 154 err = a.HandleServiceFlag("start") 155 assert.Nil(t, err, UNEXPECTED_ERR) 156 assert.True(t, a.service.(*mockDaemon).startCalled) 157 158 // Stop 159 a = newMockAgentService() 160 err = a.HandleServiceFlag("stop") 161 assert.Nil(t, err, UNEXPECTED_ERR) 162 assert.True(t, a.service.(*mockDaemon).stopCalled) 163 164 // Logs 165 a = newMockAgentService() 166 err = a.HandleServiceFlag("logs") 167 assert.Nil(t, err, UNEXPECTED_ERR) 168 assert.True(t, a.service.(*mockDaemon).logsCalled) 169 170 // Status 171 a = newMockAgentService() 172 err = a.HandleServiceFlag("status") 173 assert.Nil(t, err, UNEXPECTED_ERR) 174 assert.True(t, a.service.(*mockDaemon).statusCalled) 175 176 // Enable 177 a = newMockAgentService() 178 err = a.HandleServiceFlag("enable") 179 assert.Nil(t, err, UNEXPECTED_ERR) 180 assert.True(t, a.service.(*mockDaemon).enableCalled) 181 182 // Service Name 183 a = newMockAgentService() 184 err = a.HandleServiceFlag("name") 185 assert.Nil(t, err, UNEXPECTED_ERR) 186 assert.True(t, a.service.(*mockDaemon).serviceNameCalled) 187 }