...

Source file src/github.com/chaos-mesh/chaos-mesh/e2e-test/cmd/e2e_helper/child_process_time_server.go

Documentation: github.com/chaos-mesh/chaos-mesh/e2e-test/cmd/e2e_helper

     1  // Copyright 2021 Chaos Mesh Authors.
     2  //
     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 main
    17  
    18  import (
    19  	"context"
    20  	"io/ioutil"
    21  	"net/http"
    22  	"os"
    23  	"os/exec"
    24  	"time"
    25  )
    26  
    27  type childProcessTimeServer interface {
    28  	Start(ctx context.Context) error
    29  	Time() (*time.Time, error)
    30  }
    31  
    32  var _ childProcessTimeServer = (*defaultChildProcessTimeServer)(nil)
    33  
    34  // the default implementation would create another instance of e2e-helper, but bind with a different port
    35  type defaultChildProcessTimeServer struct {
    36  }
    37  
    38  // Start implements childProcessTimeServer
    39  func (s *defaultChildProcessTimeServer) Start(ctx context.Context) error {
    40  	selfPath, err := os.Executable()
    41  	if err != nil {
    42  		return err
    43  	}
    44  	return exec.CommandContext(ctx, selfPath, "--port", "8081").Run()
    45  }
    46  
    47  // Time implements childProcessTimeServer
    48  func (s *defaultChildProcessTimeServer) Time() (*time.Time, error) {
    49  	resp, err := http.Get("http://localhost:8081/time")
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  	defer resp.Body.Close()
    54  	bytes, err := ioutil.ReadAll(resp.Body)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  	time, err := time.Parse(time.RFC3339Nano, string(bytes))
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  	return &time, nil
    63  }
    64