github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/stream/listener_test.go (about)

     1  /*
     2  Copyright 2017 Mirantis
     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 stream
    18  
    19  import (
    20  	"fmt"
    21  	"io/ioutil"
    22  	"os"
    23  	"path/filepath"
    24  	"testing"
    25  	"time"
    26  
    27  	testutils "github.com/Mirantis/virtlet/pkg/utils/testing"
    28  )
    29  
    30  func getServer() *UnixServer {
    31  	baseDir, _ := ioutil.TempDir("", "virtlet-log")
    32  	os.Mkdir(baseDir, 0777)
    33  	socketPath := filepath.Join(baseDir, "streamer.sock")
    34  	return NewUnixServer(socketPath)
    35  }
    36  
    37  func TestAddOutputReader(t *testing.T) {
    38  	u := getServer()
    39  	ch1 := make(chan []byte, 1)
    40  	ch2 := make(chan []byte, 1)
    41  	u.AddOutputReader("1", ch1)
    42  	u.AddOutputReader("1", ch2)
    43  	outputReaders, ok := u.outputReaders["1"]
    44  	if ok == false {
    45  		t.Fatalf("No registered readers for podUID=1")
    46  	}
    47  	if len(outputReaders) != 2 {
    48  		t.Errorf("Wrong numbers of readers. Expected 2, got: %d", len(outputReaders))
    49  	}
    50  }
    51  
    52  func TestBroadcastMessageToManyReceivers(t *testing.T) {
    53  	u := getServer()
    54  	ch1 := make(chan []byte, 1)
    55  	ch2 := make(chan []byte, 1)
    56  	u.AddOutputReader("1", ch1)
    57  	u.AddOutputReader("1", ch2)
    58  
    59  	msg := []byte("test")
    60  	u.broadcast("1", msg)
    61  
    62  	select {
    63  	case m1 := <-ch1:
    64  		if string(m1) != string(msg) {
    65  			t.Errorf("channel received wrong message. Expected: %s, got: %s", msg, m1)
    66  		}
    67  	default:
    68  		t.Errorf("channel did not receive message")
    69  	}
    70  	select {
    71  	case m1 := <-ch2:
    72  		if string(m1) != string(msg) {
    73  			t.Errorf("channel received wrong message. Expected: %s, got: %s", msg, m1)
    74  		}
    75  	default:
    76  		t.Errorf("channel did not receive message")
    77  	}
    78  }
    79  
    80  func TestStopListen(t *testing.T) {
    81  	// setup server
    82  	u := getServer()
    83  	go u.Listen()
    84  	err := waitForSocket(u.SocketPath, 5)
    85  	if err != nil {
    86  		t.Errorf("Error when waiting for socket to be created: %s", u.SocketPath)
    87  	}
    88  
    89  	// stop listening
    90  	close(u.closeCh)
    91  
    92  	passed := 0
    93  loop:
    94  	for {
    95  		select {
    96  		case <-u.listenDone:
    97  			break loop
    98  		default:
    99  			if passed > 5 {
   100  				t.Fatal("Listener didn't finsh in 5 seconds")
   101  			}
   102  			time.Sleep(time.Duration(1) * time.Second)
   103  			passed++
   104  		}
   105  	}
   106  
   107  	if len(u.outputReaders) > 0 {
   108  		t.Errorf("Error after closing listener. outputReaders should be empty but contains: %v", u.outputReaders)
   109  	}
   110  }
   111  
   112  func TestCleaningReader(t *testing.T) {
   113  	// setup server
   114  	u := getServer()
   115  	go u.Listen()
   116  	err := waitForSocket(u.SocketPath, 5)
   117  	if err != nil {
   118  		t.Errorf("Error when waiting for socket to be created: %s", u.SocketPath)
   119  	}
   120  
   121  	// setup client
   122  	containerID := "1123ab2-baed-32e7-6d1d-13110da12345"
   123  	tc := testutils.RunProcess(t, "nc", []string{"-U", u.SocketPath}, []string{
   124  		"VIRTLET_CONTAINER_LOG_PATH=/var/log/pods/8c8e8cf1-acea-11e7-8e0e-02420ac00002/ubuntu_0.log",
   125  		fmt.Sprintf("VIRTLET_CONTAINER_ID=%s", containerID),
   126  	})
   127  	defer tc.Stop()
   128  
   129  	u.Stop()
   130  	outputReaders, ok := u.outputReaders[containerID]
   131  	if ok == true {
   132  		t.Errorf("Error after closing connection. outputReaders should be deleted but exists and contains: %v", outputReaders)
   133  	}
   134  }
   135  
   136  func waitForSocket(path string, timeout int) error {
   137  	passed := 0
   138  	for {
   139  		if _, err := os.Stat(path); !os.IsNotExist(err) {
   140  			break
   141  		}
   142  		time.Sleep(time.Duration(1) * time.Second)
   143  		passed++
   144  		if passed >= timeout {
   145  			return fmt.Errorf("Timeout")
   146  		}
   147  	}
   148  	return nil
   149  }