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