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" |
Mohamed Abukar | 67a790a | 2020-11-05 09:54:19 +0200 | [diff] [blame^] | 30 | "sync" |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 31 | "testing" |
| 32 | "time" |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 33 | |
| 34 | "github.com/stretchr/testify/suite" |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 35 | ) |
| 36 | |
| 37 | type do func(w http.ResponseWriter) |
| 38 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 39 | type QueryXAppsConfigTestSuite struct { |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 40 | suite.Suite |
| 41 | listener net.Listener |
| 42 | xAppMgrFunc do |
Mohamed Abukar | 67a790a | 2020-11-05 09:54:19 +0200 | [diff] [blame^] | 43 | mu sync.Mutex |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | // suite setup creates the HTTP server |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 47 | func (suite *QueryXAppsConfigTestSuite) SetupSuite() { |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 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 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 56 | func runXAppMgr(listener net.Listener, url string, suite *QueryXAppsConfigTestSuite) { |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 57 | |
| 58 | http.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) { |
| 59 | switch r.Method { |
| 60 | case "GET": |
Mohamed Abukar | 67a790a | 2020-11-05 09:54:19 +0200 | [diff] [blame^] | 61 | suite.mu.Lock() |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 62 | suite.xAppMgrFunc(w) |
Mohamed Abukar | 67a790a | 2020-11-05 09:54:19 +0200 | [diff] [blame^] | 63 | suite.mu.Unlock() |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 64 | } |
| 65 | }) |
| 66 | http.Serve(listener, nil) |
| 67 | } |
| 68 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 69 | func (suite *QueryXAppsConfigTestSuite) TestQueryXAppsConfigFailsWithTimeout() { |
| 70 | doSleep := func(w http.ResponseWriter) { |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 71 | time.Sleep(time.Second * 2) |
| 72 | } |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 73 | suite.xAppMgrFunc = doSleep |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 74 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 75 | data, err := queryXAppsConfig("http://"+suite.listener.Addr().String()+"/test_url/", 1) |
| 76 | suite.Equal([]byte("{}"), data) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 77 | suite.NotNil(err) |
| 78 | e, ok := err.(*url.Error) |
| 79 | suite.Equal(ok, true) |
| 80 | suite.Equal(e.Timeout(), true) |
| 81 | } |
| 82 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 83 | func (suite *QueryXAppsConfigTestSuite) TestQueryXAppsConfigFailsWithAnErrorReply() { |
| 84 | doReplyWithErr := func(w http.ResponseWriter) { |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 85 | http.Error(w, "405 method not allowed", http.StatusMethodNotAllowed) |
| 86 | } |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 87 | suite.xAppMgrFunc = doReplyWithErr |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 88 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 89 | data, err := queryXAppsConfig("http://"+suite.listener.Addr().String()+"/test_url/", 1) |
| 90 | suite.Equal([]byte("{}"), data) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 91 | suite.NotNil(err) |
| 92 | suite.Equal("405 Method Not Allowed", err.Error()) |
| 93 | } |
| 94 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 95 | func (suite *QueryXAppsConfigTestSuite) TestQueryXAppsConfigOk() { |
| 96 | doReply := func(w http.ResponseWriter) { |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 97 | fmt.Fprintf(w, "reply message") |
| 98 | } |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 99 | suite.xAppMgrFunc = doReply |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 100 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 101 | data, err := queryXAppsConfig("http://"+suite.listener.Addr().String()+"/test_url/", 1) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 102 | suite.NotNil(data) |
| 103 | suite.Nil(err) |
| 104 | suite.Equal(data, []byte("reply message")) |
| 105 | } |
| 106 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 107 | func TestQueryXAppsConfigTestSuite(t *testing.T) { |
| 108 | suite.Run(t, new(QueryXAppsConfigTestSuite)) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 109 | } |