github.com/jkawamoto/roadie-azure@v0.3.5/roadie/logger_test.go (about)

     1  //
     2  // roadie/logger_test.go
     3  //
     4  // Copyright (c) 2017 Junpei Kawamoto
     5  //
     6  // This file is part of Roadie Azure.
     7  //
     8  // Roadie Azure is free software: you can redistribute it and/or modify
     9  // it under the terms of the GNU General Public License as published by
    10  // the Free Software Foundation, either version 3 of the License, or
    11  // (at your option) any later version.
    12  //
    13  // Roadie Azure is distributed in the hope that it will be useful,
    14  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  // GNU General Public License for more details.
    17  //
    18  // You should have received a copy of the GNU General Public License
    19  // along with Roadie Azure. If not, see <http://www.gnu.org/licenses/>.
    20  //
    21  
    22  package roadie
    23  
    24  import (
    25  	"context"
    26  	"fmt"
    27  	"io/ioutil"
    28  	"log"
    29  	"testing"
    30  
    31  	"github.com/jkawamoto/roadie/cloud/azure"
    32  	"github.com/jkawamoto/roadie/cloud/azure/mock"
    33  )
    34  
    35  func TestLogWriter(t *testing.T) {
    36  
    37  	server := mock.NewStorageServer()
    38  	defer server.Close()
    39  
    40  	cli, err := server.GetClient()
    41  	if err != nil {
    42  		t.Fatalf("cannot get a client: %v", err)
    43  	}
    44  
    45  	store := azure.StorageService{
    46  		Client: cli.GetBlobService(),
    47  		Logger: log.New(ioutil.Discard, "", log.LstdFlags),
    48  	}
    49  
    50  	testName := "test-name"
    51  	var expected string
    52  	log := NewLogWriter(context.Background(), &store, testName, nil)
    53  	for i := 0; i != 10; i++ {
    54  		msg := fmt.Sprintf("msg,%v\n", i)
    55  		fmt.Fprint(log, msg)
    56  		expected += msg
    57  	}
    58  	err = log.Close()
    59  	if err != nil {
    60  		t.Fatalf("cannot close a log writer: %v", err)
    61  	}
    62  	c, ok := server.Items["log"]
    63  	if !ok {
    64  		t.Fatal("container logged files are stored doesn't found")
    65  	}
    66  	f, ok := c[testName]
    67  	if !ok {
    68  		t.Fatalf("log file doesn't exist")
    69  	}
    70  	if f.Body != expected {
    71  		t.Errorf("stored log file os %q, want %q", f.Body, expected)
    72  	}
    73  
    74  }