github.com/TugasAkhir-QUIC/quic-go@v0.0.2-0.20240215011318-d20e25a9054c/qlog/qlog_dir_test.go (about)

     1  package qlog
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"path"
     7  
     8  	. "github.com/onsi/ginkgo/v2"
     9  	. "github.com/onsi/gomega"
    10  	"github.com/TugasAkhir-QUIC/quic-go/internal/protocol"
    11  	"github.com/TugasAkhir-QUIC/quic-go/logging"
    12  )
    13  
    14  var _ = Describe("qlog dir tests", Serial, func() {
    15  	var originalQlogDirValue string
    16  	var tempTestDirPath string
    17  	ctx := context.Background()
    18  	perspective := logging.PerspectiveClient
    19  	connID, _ := protocol.GenerateConnectionIDForInitial()
    20  
    21  	BeforeEach(func() {
    22  		originalQlogDirValue = os.Getenv("QLOGDIR")
    23  		var err error
    24  		tempTestDirPath, err = os.MkdirTemp("", "temp_test_dir")
    25  		Expect(err).ToNot(HaveOccurred())
    26  	})
    27  
    28  	AfterEach(func() {
    29  		err := os.Setenv("QLOGDIR", originalQlogDirValue)
    30  		Expect(err).ToNot(HaveOccurred())
    31  		err = os.RemoveAll(tempTestDirPath)
    32  		Expect(err).ToNot(HaveOccurred())
    33  	})
    34  
    35  	It("environment variable is set", func() {
    36  		qlogDir := path.Join(tempTestDirPath, "qlogs")
    37  		err := os.Setenv("QLOGDIR", qlogDir)
    38  		Expect(err).ToNot(HaveOccurred())
    39  		tracer := DefaultTracer(ctx, perspective, connID)
    40  		Expect(tracer).ToNot(BeNil())
    41  		tracer.Close()
    42  		_, err = os.Stat(qlogDir)
    43  		qlogDirCreated := !os.IsNotExist(err)
    44  		Expect(qlogDirCreated).To(BeTrue())
    45  		childs, err := os.ReadDir(qlogDir)
    46  		Expect(err).ToNot(HaveOccurred())
    47  		Expect(len(childs)).To(Equal(1))
    48  	})
    49  
    50  	It("environment variable is not set", func() {
    51  		err := os.Setenv("QLOGDIR", "")
    52  		Expect(err).ToNot(HaveOccurred())
    53  		tracer := DefaultTracer(ctx, perspective, connID)
    54  		Expect(tracer).To(BeNil())
    55  	})
    56  })