airshipui/internal/webservice/alerts_test.go
Matthew Fuller 7548c4bb9c Add testing to airshipui
1. Change embedded structs to pointers so json omitempty will
behave as expected and omit empty structs from ws messages
2. Add dummy airshipui.json config file for testing
3. Add testutil package to build dummy config objects
4. Add tests for configs package
5. Add tests for webservice package
6. Add tests for integrations/ctl package

Change-Id: I1a2ac543898cbbae96c764a983e7e9b73946a9d9
2020-06-19 14:33:42 -07:00

77 lines
1.8 KiB
Go

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package webservice
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"opendev.org/airship/airshipui/internal/configs"
)
func TestSendAlert(t *testing.T) {
client, err := NewTestClient()
require.NoError(t, err)
defer client.Close()
// construct and send alert from server to client
SendAlert(configs.Error, "Test Alert", true)
var response configs.WsMessage
err = client.ReadJSON(&response)
require.NoError(t, err)
expected := configs.WsMessage{
Type: configs.Alert,
Component: configs.Error,
Message: "Test Alert",
Fade: true,
// don't fail on timestamp diff
Timestamp: response.Timestamp,
}
assert.Equal(t, expected, response)
}
func TestSendAlertNoWebSocket(t *testing.T) {
// test requires that ws == nil
conn := ws
ws = nil
defer func() {
ws = conn
Alerts = nil
}()
// queue should be empty
Alerts = nil
SendAlert(configs.Info, "Test Alert", true)
// ws is nil, so the queue should now have 1 Alert
assert.Len(t, Alerts, 1)
expected := configs.WsMessage{
Type: configs.Alert,
Component: configs.Info,
Message: "Test Alert",
Fade: true,
// don't fail on timestamp diff
Timestamp: Alerts[0].Timestamp,
}
assert.Equal(t, expected, Alerts[0])
}