github.com/noisysockets/noisysockets@v0.21.2-0.20240515114641-7f467e651c90/config/config_test.go (about) 1 // SPDX-License-Identifier: MPL-2.0 2 /* 3 * Copyright (C) 2024 The Noisy Sockets Authors. 4 * 5 * This Source Code Form is subject to the terms of the Mozilla Public 6 * License, v. 2.0. If a copy of the MPL was not distributed with this 7 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 8 */ 9 10 package config_test 11 12 import ( 13 "bytes" 14 "os" 15 "testing" 16 17 "github.com/noisysockets/noisysockets/config" 18 "github.com/stretchr/testify/require" 19 ) 20 21 func TestFromYAML(t *testing.T) { 22 configFile, err := os.Open("testdata/config_v1alpha2.yaml") 23 require.NoError(t, err) 24 t.Cleanup(func() { 25 require.NoError(t, configFile.Close()) 26 }) 27 28 conf, err := config.FromYAML(configFile) 29 require.NoError(t, err) 30 31 require.Equal(t, "Config", conf.GetKind()) 32 require.Equal(t, "noisysockets.github.com/v1alpha2", conf.GetAPIVersion()) 33 34 // Just check a few fields to make sure the config was parsed correctly. 35 require.Equal(t, uint16(12345), conf.ListenPort) 36 require.Equal(t, "6cvvZyj+EVL4DHjUKeVF7EUBfgR2mJO4php2Gdv9FVw=", conf.Peers[0].PublicKey) 37 } 38 39 func TestToYAML(t *testing.T) { 40 configFile, err := os.Open("testdata/config_v1alpha2.yaml") 41 require.NoError(t, err) 42 t.Cleanup(func() { 43 require.NoError(t, configFile.Close()) 44 }) 45 46 conf, err := config.FromYAML(configFile) 47 require.NoError(t, err) 48 49 var buf bytes.Buffer 50 err = config.ToYAML(&buf, conf) 51 require.NoError(t, err) 52 53 conf2, err := config.FromYAML(bytes.NewReader(buf.Bytes())) 54 require.NoError(t, err) 55 56 require.Equal(t, conf, conf2) 57 } 58 59 func TestMigration(t *testing.T) { 60 configFile, err := os.Open("testdata/config_v1alpha1.yaml") 61 require.NoError(t, err) 62 t.Cleanup(func() { 63 require.NoError(t, configFile.Close()) 64 }) 65 66 conf, err := config.FromYAML(configFile) 67 require.NoError(t, err) 68 69 var migratedConf bytes.Buffer 70 err = config.ToYAML(&migratedConf, conf) 71 require.NoError(t, err) 72 73 expectedConf, err := os.ReadFile("testdata/migrated_v1alpha1.yaml") 74 require.NoError(t, err) 75 76 require.YAMLEq(t, string(expectedConf), migratedConf.String()) 77 }