github.com/replicatedhq/ship@v0.55.0/pkg/lifecycle/daemon/daemon_channel_test.go (about) 1 package daemon 2 3 import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 "io/ioutil" 8 "math/rand" 9 "net/http" 10 "testing" 11 "time" 12 13 "github.com/gin-gonic/gin" 14 "github.com/mitchellh/cli" 15 "github.com/replicatedhq/ship/pkg/api" 16 "github.com/replicatedhq/ship/pkg/lifecycle/kustomize" 17 "github.com/replicatedhq/ship/pkg/testing/logger" 18 "github.com/spf13/afero" 19 "github.com/spf13/viper" 20 "github.com/stretchr/testify/require" 21 ) 22 23 type daemonChannelTest struct { 24 name string 25 release *api.Release 26 expectName string 27 expectIcon string 28 } 29 30 func TestDaemonChannel(t *testing.T) { 31 gin.SetMode(gin.ReleaseMode) 32 tests := []daemonChannelTest{ 33 { 34 name: "test_channel_noicon", 35 release: &api.Release{ 36 Metadata: api.ReleaseMetadata{ 37 Type: "replicated.app", 38 ChannelName: "Application", 39 ChannelIcon: "", 40 }, 41 }, 42 expectName: "Application", 43 expectIcon: "", 44 }, 45 { 46 name: "test_channel_withicon", 47 release: &api.Release{ 48 Metadata: api.ReleaseMetadata{ 49 Type: "replicated.app", 50 ChannelName: "Clubhouse Enterprise", 51 ChannelIcon: "https://frontend-production-cdn.clubhouse.io/v0.5.20180509155736/images/logos/clubhouse_mascot_180x180.png", 52 }, 53 }, 54 expectName: "Clubhouse Enterprise", 55 expectIcon: "https://frontend-production-cdn.clubhouse.io/v0.5.20180509155736/images/logos/clubhouse_mascot_180x180.png", 56 }, 57 } 58 for _, test := range tests { 59 t.Run(test.name, func(t *testing.T) { 60 req := require.New(t) 61 62 v := viper.New() 63 64 port := rand.Intn(2000) + 33000 65 viper.Set("api-port", port) 66 fs := afero.Afero{Fs: afero.NewMemMapFs()} 67 log := &logger.TestLogger{T: t} 68 daemon := &ShipDaemon{ 69 Logger: log, 70 WebUIFactory: WebUIFactoryFactory(log), 71 Viper: v, 72 V1Routes: &V1Routes{ 73 Logger: log, 74 Fs: fs, 75 Viper: v, 76 77 UI: cli.NewMockUi(), 78 OpenWebConsole: func(ui cli.Ui, s string, b bool) error { return nil }, 79 }, 80 NavcycleRoutes: &NavcycleRoutes{ 81 Kustomizer: &kustomize.Kustomizer{}, 82 Shutdown: make(chan interface{}), 83 }, 84 } 85 86 daemonCtx, daemonCancelFunc := context.WithCancel(context.Background()) 87 88 errChan := make(chan error) 89 log.Log("starting daemon") 90 go func(errCh chan error) { 91 err := daemon.Serve(daemonCtx, test.release) 92 log.Log("daemon.error", err) 93 errCh <- err 94 }(errChan) 95 96 // sigh. Give the server a second to start up 97 time.Sleep(500 * time.Millisecond) 98 99 resp, err := http.Get(fmt.Sprintf("http://localhost:%d/api/v1/metadata", port)) 100 req.NoError(err) 101 102 body, err := ioutil.ReadAll(resp.Body) 103 req.NoError(err) 104 log.Log("received body", fmt.Sprintf("\"%s\"", body)) 105 106 daemonCancelFunc() 107 108 unmarshalled := make(map[string]string) 109 err = json.Unmarshal(body, &unmarshalled) 110 req.NoError(err) 111 112 req.Equal(test.expectName, unmarshalled["name"]) 113 req.Equal(test.expectIcon, unmarshalled["icon"]) 114 115 daemonErr := <-errChan 116 req.EqualError(daemonErr, "context canceled") 117 }) 118 } 119 }