Skip to content

Commit

Permalink
git merge upstream/master
Browse files Browse the repository at this point in the history
  • Loading branch information
wafuwafu13 committed Sep 7, 2022
2 parents 92d3d5f + 857b47d commit 3b91f06
Show file tree
Hide file tree
Showing 25 changed files with 64 additions and 110 deletions.
2 changes: 1 addition & 1 deletion agent/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func generateValues(generators []metrics.Generator) []*metrics.ValuesCustomIdent

startedAt := time.Now()
values, err := g.Generate()
if seconds := (time.Now().Sub(startedAt) / time.Second); seconds > 120 {
if seconds := (time.Since(startedAt) / time.Second); seconds > 120 {
logger.Warningf("%T.Generate() take a long time (%d seconds)", g, seconds)
}
if err != nil {
Expand Down
12 changes: 2 additions & 10 deletions command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package command
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"sort"
"sync"
"testing"
Expand Down Expand Up @@ -90,19 +88,14 @@ func newMockAPIServer(t *testing.T) (config.Config, map[string]func(*http.Reques
fmt.Fprint(w, string(respJSON))
}))

root, err := ioutil.TempDir("", "mackerel-agent-test")
if err != nil {
t.Fatal(err)
}

root := t.TempDir()
conf := config.Config{
Apibase: ts.URL,
Root: root,
}

return conf, mockHandlers, ts, func() {
ts.Close()
os.RemoveAll(root)
}
}

Expand Down Expand Up @@ -176,8 +169,7 @@ func TestPrepareWithCreateWithFail(t *testing.T) {
func TestPrepareWithUpdate(t *testing.T) {
conf, mockHandlers, ts, deferFunc := newMockAPIServer(t)
defer deferFunc()
tempDir, _ := ioutil.TempDir("", "")
conf.Root = tempDir
conf.Root = t.TempDir()
conf.SaveHostID("xxx12345678901")

mockHandlers["PUT /api/v0/hosts/xxx12345678901"] = func(req *http.Request) (int, jsonObject) {
Expand Down
9 changes: 4 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -436,10 +435,10 @@ func LoadConfig(conffile string) (*Config, error) {
if config.Pidfile == "" {
config.Pidfile = DefaultConfig.Pidfile
}
if config.Verbose == false {
if !config.Verbose {
config.Verbose = DefaultConfig.Verbose
}
if config.Diagnostic == false {
if !config.Diagnostic {
config.Diagnostic = DefaultConfig.Diagnostic
}

Expand Down Expand Up @@ -522,7 +521,7 @@ func includeConfigFile(config *Config, include string) error {

// If included config does not have "roles" key,
// use the previous roles configuration value.
if meta.IsDefined("roles") == false {
if !meta.IsDefined("roles") {
config.Roles = rolesSaved
}

Expand Down Expand Up @@ -582,7 +581,7 @@ func (s FileSystemHostIDStorage) HostIDFile() string {

// LoadHostID loads the current host ID from the mackerel-agent's id file.
func (s FileSystemHostIDStorage) LoadHostID() (string, error) {
content, err := ioutil.ReadFile(s.HostIDFile())
content, err := os.ReadFile(s.HostIDFile())
if err != nil {
return "", err
}
Expand Down
16 changes: 5 additions & 11 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -602,8 +601,7 @@ var tomlQuotedReplacer = strings.NewReplacer(
)

func TestLoadConfigFileInclude(t *testing.T) {
configDir, err := ioutil.TempDir("", "mackerel-config-test")
assertNoError(t, err)
configDir := t.TempDir()
t.Cleanup(func() { os.RemoveAll(configDir) })

includedFile, err := os.Create(filepath.Join(configDir, "sub1.conf"))
Expand Down Expand Up @@ -659,8 +657,7 @@ command = "bar"
}

func TestLoadConfigFileIncludeOverwritten(t *testing.T) {
configDir, err := ioutil.TempDir("", "mackerel-config-test")
assertNoError(t, err)
configDir := t.TempDir()
t.Cleanup(func() { os.RemoveAll(configDir) })

includedFile, err := os.Create(filepath.Join(configDir, "sub2.conf"))
Expand Down Expand Up @@ -700,14 +697,11 @@ verbose = true
}

func TestFileSystemHostIDStorage(t *testing.T) {
root, err := ioutil.TempDir("", "mackerel-agent-test")
if err != nil {
t.Fatal(err)
}
root := t.TempDir()
t.Cleanup(func() { os.RemoveAll(root) })

s := FileSystemHostIDStorage{Root: root}
err = s.SaveHostID("test-host-id")
err := s.SaveHostID("test-host-id")
assertNoError(t, err)

hostID, err := s.LoadHostID()
Expand Down Expand Up @@ -900,7 +894,7 @@ env = { "SAMPLE_KEY1" = " foo bar ", "SAMPLE_KEY2" = " baz qux " }
}

func newTempFileWithContent(content string) (*os.File, error) {
tmpf, err := ioutil.TempFile("", "mackerel-config-test")
tmpf, err := os.CreateTemp("", "mackerel-config-test")
if err != nil {
return nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions do_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -47,13 +46,13 @@ func doInitialize(fs *flag.FlagSet, argv []string) error {
}
contents := []byte(fmt.Sprintf("apikey = %q\n", *apikey))
if confExists {
cBytes, err := ioutil.ReadFile(*conffile)
cBytes, err := os.ReadFile(*conffile)
if err != nil {
return err
}
contents = append(contents, cBytes...)
}
return ioutil.WriteFile(*conffile, contents, 0644)
return os.WriteFile(*conffile, contents, 0644)
}

type apikeyAlreadySetError string
Expand Down
9 changes: 2 additions & 7 deletions do_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"flag"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand All @@ -17,11 +16,7 @@ func TestDoInitialize(t *testing.T) {
}
}

root, err := ioutil.TempDir("", "mackerel-config-test")
if err != nil {
t.Fatalf("Could not create temporary dir for test")
}
defer os.RemoveAll(root)
root := t.TempDir()

{
// not exist config file directory
Expand All @@ -41,7 +36,7 @@ func TestDoInitialize(t *testing.T) {
if err != nil {
t.Errorf("err should be nil but: %s", err)
}
content, _ := ioutil.ReadFile(conffile)
content, _ := os.ReadFile(conffile)
if string(content) != `apikey = "hoge"`+"\n" {
t.Errorf("somthing went wrong: %s", string(content))
}
Expand Down
4 changes: 2 additions & 2 deletions mackerel/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package mackerel
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"reflect"
Expand All @@ -22,7 +22,7 @@ func TestReportCheckMonitors(t *testing.T) {
t.Error("request method should be POST but :", req.Method)
}

body, _ := ioutil.ReadAll(req.Body)
body, _ := io.ReadAll(req.Body)
content := string(body)
type testrepo struct {
Source map[string]string `json:"source"`
Expand Down
19 changes: 9 additions & 10 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"math"
"os"
"os/signal"
Expand All @@ -18,7 +17,7 @@ import (

func TestParseFlags(t *testing.T) {
// prepare dummy config
confFile, err := ioutil.TempFile("", "mackerel-config-test")
confFile, err := os.CreateTemp("", "mackerel-config-test")

if err != nil {
t.Fatalf("Could not create temporary config file for test")
Expand Down Expand Up @@ -60,7 +59,7 @@ diagnostic=false

func TestDetectForce(t *testing.T) {
// prepare dummy config
confFile, err := ioutil.TempFile("", "mackerel-config-test")
confFile, err := os.CreateTemp("", "mackerel-config-test")
if err != nil {
t.Fatalf("Could not create temporary config file for test")
}
Expand Down Expand Up @@ -90,7 +89,7 @@ func TestDetectForce(t *testing.T) {
}

func TestResolveConfigForRetire(t *testing.T) {
confFile, err := ioutil.TempFile("", "mackerel-config-test")
confFile, err := os.CreateTemp("", "mackerel-config-test")
if err != nil {
t.Fatalf("Could not create temporary config file for test")
}
Expand Down Expand Up @@ -123,7 +122,7 @@ func TestResolveConfigForRetire(t *testing.T) {
}

func TestCreateAndRemovePidFile(t *testing.T) {
file, err := ioutil.TempFile("", "")
file, err := os.CreateTemp("", "")
if err != nil {
t.Errorf("failed to create tmpfile, %s", err)
}
Expand All @@ -141,7 +140,7 @@ func TestCreateAndRemovePidFile(t *testing.T) {
}

pidfile.Remove(fpath)
ioutil.WriteFile(fpath, []byte(fmt.Sprint(math.MaxInt32)), 0644)
os.WriteFile(fpath, []byte(fmt.Sprint(math.MaxInt32)), 0644)
if err := pidfile.Create(fpath); err != nil {
t.Errorf("old pid file should be ignored and new pid file should be created but, %s", err)
}
Expand Down Expand Up @@ -204,7 +203,7 @@ func TestNotifyUpdateFileDelete(t *testing.T) {
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGHUP)
go signalHandler(c, app, termCh)

f, err := ioutil.TempFile("", "mackerel-agent.test.*")
f, err := os.CreateTemp("", "mackerel-agent.test.*")
if err != nil {
t.Fatalf("can't create a temporary file: %v", err)
}
Expand Down Expand Up @@ -266,7 +265,7 @@ func TestConfigProxy(t *testing.T) {

func TestConfigTestOK(t *testing.T) {
// prepare dummy config
confFile, err := ioutil.TempFile("", "mackerel-config-test")
confFile, err := os.CreateTemp("", "mackerel-config-test")
if err != nil {
t.Fatalf("Could not create temporary config file for test")
}
Expand All @@ -286,7 +285,7 @@ func TestConfigTestOK(t *testing.T) {

func TestConfigTestNotFound(t *testing.T) {
// prepare dummy config
confFile, err := ioutil.TempFile("", "mackerel-config-test")
confFile, err := os.CreateTemp("", "mackerel-config-test")
if err != nil {
t.Fatalf("Could not create temporary config file for test")
}
Expand All @@ -306,7 +305,7 @@ func TestConfigTestNotFound(t *testing.T) {

func TestConfigTestInvalidFormat(t *testing.T) {
// prepare dummy config
confFile, err := ioutil.TempFile("", "mackerel-config-test")
confFile, err := os.CreateTemp("", "mackerel-config-test")
if err != nil {
t.Fatalf("Could not create temporary config file for test")
}
Expand Down
5 changes: 2 additions & 3 deletions metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package metadata
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -59,7 +58,7 @@ func (g *Generator) IsChanged(metadata interface{}) bool {

// LoadFromFile loads the previous metadata from file
func (g *Generator) LoadFromFile() {
data, err := ioutil.ReadFile(g.Cachefile)
data, err := os.ReadFile(g.Cachefile)
if err != nil { // maybe initial state
return
}
Expand Down Expand Up @@ -100,7 +99,7 @@ func (g *Generator) Clear() error {
// writeFileAtomically writes contents to the file atomically
func writeFileAtomically(f string, contents []byte) error {
// MUST be located on same disk partition
tmpf, err := ioutil.TempFile(filepath.Dir(f), "tmp")
tmpf, err := os.CreateTemp(filepath.Dir(f), "tmp")
if err != nil {
return err
}
Expand Down
5 changes: 1 addition & 4 deletions metrics/freebsd/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ func (g *MemoryGenerator) Generate() (metrics.Values, error) {
for i := 1; i < len(fields); i += 2 {
v, err := getValue(fields[i])
if err == nil {
k := fields[i+1]
if strings.HasSuffix(k, ",") {
k = k[0 : len(k)-1]
}
k := strings.TrimSuffix(fields[i+1], ",")
switch k {
case "Cache":
ret["memory.cached"] = v
Expand Down
4 changes: 2 additions & 2 deletions metrics/linux/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -87,7 +87,7 @@ func (g *DiskGenerator) Generate() (metrics.Values, error) {
}

func (g *DiskGenerator) collectDiskstatValues() (metrics.Values, error) {
out, err := ioutil.ReadFile("/proc/diskstats")
out, err := os.ReadFile("/proc/diskstats")
if err != nil {
diskLogger.Errorf("Failed (skip these metrics): %s", err)
return nil, err
Expand Down
5 changes: 1 addition & 4 deletions metrics/netbsd/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ func (g *MemoryGenerator) Generate() (metrics.Values, error) {
for i := 1; i < len(fields); i += 2 {
v, err := getValue(fields[i])
if err == nil {
k := fields[i+1]
if strings.HasSuffix(k, ",") {
k = k[0 : len(k)-1]
}
k := strings.TrimSuffix(fields[i+1], ",")
switch k {
case "Act":
ret["memory.act"] = v
Expand Down
2 changes: 1 addition & 1 deletion metrics/windows/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (g *DiskGenerator) queryWmiWithTimeout() ([]win32PerfFormattedDataPerfDiskP
}()
select {
case <-time.After(queryWmiTimeout):
return nil, errors.New("Timeouted while retrieving disk metrics")
return nil, errors.New("timeouted while retrieving disk metrics")
case err := <-errCh:
return nil, err
case records := <-recordsCh:
Expand Down
Loading

0 comments on commit 3b91f06

Please sign in to comment.