Improve O1 agent UT coverage
[ric-plt/o1.git] / agent / cmd / o1agent_test.go
1 /*
2 ==================================================================================
3   Copyright (c) 2020 AT&T Intellectual Property.
4   Copyright (c) 2020 Nokia
5
6    Licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8    You may obtain a copy of the License at
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17 ==================================================================================
18 */
19
20 package main
21
22 import (
23         "github.com/stretchr/testify/assert"
24         "os"
25         "syscall"
26         "testing"
27         "time"
28 )
29
30 var o1Agent *O1Agent
31
32 // Test cases
33 func TestMain(M *testing.M) {
34         o1Agent = NewO1Agent()
35         go func() {
36                 o1Agent.nbiClient.Start()
37                 o1Agent.Run()
38         }()
39         time.Sleep(time.Duration(1) * time.Second)
40         os.Exit(M.Run())
41 }
42
43 func TestConsume(t *testing.T) {
44         ret := o1Agent.Consume(nil)
45         assert.Nil(t, ret)
46 }
47
48 func TestConfigChangeHandler(t *testing.T) {
49         o1Agent.ConfigChangeHandler("")
50 }
51
52 func TestStatusCB(t *testing.T) {
53         assert.True(t, o1Agent.StatusCB())
54 }
55
56 func TestSighandler(t *testing.T) {
57         oldOsExit := osExit
58         defer func() { osExit = oldOsExit }()
59
60         var exitCode int
61         osExit = func(code int) {
62                 exitCode = code
63         }
64
65         go o1Agent.Sighandler()
66         o1Agent.sigChan <- syscall.SIGTERM
67         time.Sleep(time.Duration(1) * time.Second)
68         assert.Equal(t, 1, exitCode)
69 }