github.com/nats-io/nsc@v0.0.0-20221206222106-35db9400b257/cmd/natsresolverconfigbuilder_test.go (about)

     1  /*
     2   * Copyright 2018-2020 The NATS Authors
     3   * Licensed under the Apache License, Version 2.0 (the "License");
     4   * you may not use this file except in compliance with the License.
     5   * You may obtain a copy of the License at
     6   *
     7   * http://www.apache.org/licenses/LICENSE-2.0
     8   *
     9   * Unless required by applicable law or agreed to in writing, software
    10   * distributed under the License is distributed on an "AS IS" BASIS,
    11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12   * See the License for the specific language governing permissions and
    13   * limitations under the License.
    14   */
    15  
    16  package cmd
    17  
    18  import (
    19  	"bytes"
    20  	"fmt"
    21  	"os"
    22  	"path/filepath"
    23  	"testing"
    24  
    25  	"github.com/nats-io/nats-server/v2/server"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func Test_NatsResolverServerParse(t *testing.T) {
    30  	ts := NewEmptyStore(t)
    31  	defer ts.Done(t)
    32  	_, _, err := ExecuteCmd(createAddOperatorCmd(), "--name", "OP", "--sys")
    33  	require.NoError(t, err)
    34  	serverconf := filepath.Join(ts.Dir, "server.conf")
    35  	_, _, err = ExecuteCmd(createServerConfigCmd(), "--nats-resolver", "--config-file", serverconf)
    36  	require.NoError(t, err)
    37  	// modify the generated file so the jwt directory does not get created where the test is running
    38  	data, err := os.ReadFile(serverconf)
    39  	require.NoError(t, err)
    40  	dir, err := os.MkdirTemp("", "Test_NatsResolverServerParse-jwt-")
    41  	require.NoError(t, err)
    42  	defer os.Remove(dir)
    43  	data = bytes.ReplaceAll(data, []byte(`dir: './jwt'`), []byte(fmt.Sprintf(`dir: '%s'`, dir)))
    44  	err = os.WriteFile(serverconf, data, 0660)
    45  	require.NoError(t, err)
    46  	// test parsing
    47  	var opts server.Options
    48  	require.NoError(t, opts.ProcessConfigFile(serverconf))
    49  	require.NotEmpty(t, opts.SystemAccount)
    50  	require.NotEmpty(t, opts.TrustedOperators)
    51  }