Fix race condition in UT cases
[ric-plt/vespamgr.git] / cmd / vesmgr / vesmgr_queryxappconfig_test.go
1 /*
2  *  Copyright (c) 2019 AT&T Intellectual Property.
3  *  Copyright (c) 2018-2019 Nokia.
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  *  This source code is part of the near-RT RIC (RAN Intelligent Controller)
18  *  platform project (RICP).
19  *
20  */
21
22 package main
23
24 import (
25         "fmt"
26         "net"
27         "net/http"
28         "net/url"
29         "os"
30         "sync"
31         "testing"
32         "time"
33
34         "github.com/stretchr/testify/suite"
35 )
36
37 type do func(w http.ResponseWriter)
38
39 type QueryXAppsConfigTestSuite struct {
40         suite.Suite
41         listener    net.Listener
42         xAppMgrFunc do
43         mu          sync.Mutex
44 }
45
46 // suite setup creates the HTTP server
47 func (suite *QueryXAppsConfigTestSuite) SetupSuite() {
48         os.Unsetenv("http_proxy")
49         os.Unsetenv("HTTP_PROXY")
50         var err error
51         suite.listener, err = net.Listen("tcp", ":0")
52         suite.Nil(err)
53         go runXAppMgr(suite.listener, "/test_url/", suite)
54 }
55
56 func runXAppMgr(listener net.Listener, url string, suite *QueryXAppsConfigTestSuite) {
57
58         http.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
59                 switch r.Method {
60                 case "GET":
61                         suite.mu.Lock()
62                         suite.xAppMgrFunc(w)
63                         suite.mu.Unlock()
64                 }
65         })
66         http.Serve(listener, nil)
67 }
68
69 func (suite *QueryXAppsConfigTestSuite) TestQueryXAppsConfigFailsWithTimeout() {
70         doSleep := func(w http.ResponseWriter) {
71                 time.Sleep(time.Second * 2)
72         }
73         suite.xAppMgrFunc = doSleep
74
75         data, err := queryXAppsConfig("http://"+suite.listener.Addr().String()+"/test_url/", 1)
76         suite.Equal([]byte("{}"), data)
77         suite.NotNil(err)
78         e, ok := err.(*url.Error)
79         suite.Equal(ok, true)
80         suite.Equal(e.Timeout(), true)
81 }
82
83 func (suite *QueryXAppsConfigTestSuite) TestQueryXAppsConfigFailsWithAnErrorReply() {
84         doReplyWithErr := func(w http.ResponseWriter) {
85                 http.Error(w, "405 method not allowed", http.StatusMethodNotAllowed)
86         }
87         suite.xAppMgrFunc = doReplyWithErr
88
89         data, err := queryXAppsConfig("http://"+suite.listener.Addr().String()+"/test_url/", 1)
90         suite.Equal([]byte("{}"), data)
91         suite.NotNil(err)
92         suite.Equal("405 Method Not Allowed", err.Error())
93 }
94
95 func (suite *QueryXAppsConfigTestSuite) TestQueryXAppsConfigOk() {
96         doReply := func(w http.ResponseWriter) {
97                 fmt.Fprintf(w, "reply message")
98         }
99         suite.xAppMgrFunc = doReply
100
101         data, err := queryXAppsConfig("http://"+suite.listener.Addr().String()+"/test_url/", 1)
102         suite.NotNil(data)
103         suite.Nil(err)
104         suite.Equal(data, []byte("reply message"))
105 }
106
107 func TestQueryXAppsConfigTestSuite(t *testing.T) {
108         suite.Run(t, new(QueryXAppsConfigTestSuite))
109 }