github.com/noisysockets/noisysockets@v0.21.2-0.20240515114641-7f467e651c90/config/wg_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 TestFromINI(t *testing.T) {
    22  	f, err := os.Open("testdata/wg0.conf")
    23  	require.NoError(t, err)
    24  
    25  	conf, err := config.FromINI(f)
    26  	require.NoError(t, err)
    27  
    28  	var buf bytes.Buffer
    29  	err = config.ToYAML(&buf, conf)
    30  	require.NoError(t, err)
    31  
    32  	expected, err := os.ReadFile("testdata/from_ini_config.yaml")
    33  	require.NoError(t, err)
    34  
    35  	require.YAMLEq(t, string(expected), buf.String())
    36  }
    37  
    38  func TestToINI(t *testing.T) {
    39  	configFile, err := os.Open("testdata/config_v1alpha2.yaml")
    40  	require.NoError(t, err)
    41  	t.Cleanup(func() {
    42  		require.NoError(t, configFile.Close())
    43  	})
    44  
    45  	conf, err := config.FromYAML(configFile)
    46  	require.NoError(t, err)
    47  
    48  	var buf bytes.Buffer
    49  	err = config.ToINI(&buf, conf)
    50  	require.NoError(t, err)
    51  
    52  	expected, err := os.ReadFile("testdata/wg0.conf")
    53  	require.NoError(t, err)
    54  
    55  	require.Equal(t, string(expected), buf.String())
    56  }