storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/event/target/nats_test.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2019 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package target
    18  
    19  import (
    20  	"testing"
    21  
    22  	natsserver "github.com/nats-io/nats-server/v2/test"
    23  
    24  	xnet "storj.io/minio/pkg/net"
    25  )
    26  
    27  func TestNatsConnPlain(t *testing.T) {
    28  	opts := natsserver.DefaultTestOptions
    29  	opts.Port = 14222
    30  	s := natsserver.RunServer(&opts)
    31  	defer s.Shutdown()
    32  
    33  	clientConfig := &NATSArgs{
    34  		Enable: true,
    35  		Address: xnet.Host{Name: "localhost",
    36  			Port:      (xnet.Port(opts.Port)),
    37  			IsPortSet: true},
    38  		Subject: "test",
    39  	}
    40  	con, err := clientConfig.connectNats()
    41  	if err != nil {
    42  		t.Errorf("Could not connect to nats: %v", err)
    43  	}
    44  	defer con.Close()
    45  }
    46  
    47  func TestNatsConnUserPass(t *testing.T) {
    48  	opts := natsserver.DefaultTestOptions
    49  	opts.Port = 14223
    50  	opts.Username = "testminio"
    51  	opts.Password = "miniotest"
    52  	s := natsserver.RunServer(&opts)
    53  	defer s.Shutdown()
    54  
    55  	clientConfig := &NATSArgs{
    56  		Enable: true,
    57  		Address: xnet.Host{Name: "localhost",
    58  			Port:      (xnet.Port(opts.Port)),
    59  			IsPortSet: true},
    60  		Subject:  "test",
    61  		Username: opts.Username,
    62  		Password: opts.Password,
    63  	}
    64  
    65  	con, err := clientConfig.connectNats()
    66  	if err != nil {
    67  		t.Errorf("Could not connect to nats: %v", err)
    68  	}
    69  	defer con.Close()
    70  }
    71  
    72  func TestNatsConnToken(t *testing.T) {
    73  	opts := natsserver.DefaultTestOptions
    74  	opts.Port = 14223
    75  	opts.Authorization = "s3cr3t"
    76  	s := natsserver.RunServer(&opts)
    77  	defer s.Shutdown()
    78  
    79  	clientConfig := &NATSArgs{
    80  		Enable: true,
    81  		Address: xnet.Host{Name: "localhost",
    82  			Port:      (xnet.Port(opts.Port)),
    83  			IsPortSet: true},
    84  		Subject: "test",
    85  		Token:   opts.Authorization,
    86  	}
    87  
    88  	con, err := clientConfig.connectNats()
    89  	if err != nil {
    90  		t.Errorf("Could not connect to nats: %v", err)
    91  	}
    92  	defer con.Close()
    93  }