Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace io/ioutil package with os package #685

Merged
merged 1 commit into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/chartmuseum/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"time"

Expand Down Expand Up @@ -186,7 +186,7 @@ func (router *Router) Start(port int) {
if router.TlsCACert != "" {
keypair, _ := tls.LoadX509KeyPair(router.TlsCert, router.TlsKey)
certpool := x509.NewCertPool()
capem, _ := ioutil.ReadFile(router.TlsCACert)
capem, _ := os.ReadFile(router.TlsCACert)
if !certpool.AppendCertsFromPEM(capem) {
router.Logger.Fatal("Can't parse CA certificate file")
}
Expand Down
43 changes: 21 additions & 22 deletions pkg/chartmuseum/server/multitenant/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -677,7 +676,7 @@ entries:
- charts/acs-engine-autoscaler-2.1.2.tgz
version: 2.1.2
generated: "2018-05-23T15:14:46-05:00"`)
err = ioutil.WriteFile(indexCacheFilePath, content, 0644)
err = os.WriteFile(indexCacheFilePath, content, 0644)
suite.Nil(err, "no error creating test index-cache.yaml")
defer os.Remove(indexCacheFilePath)

Expand All @@ -700,7 +699,7 @@ generated: "2018-05-23T15:14:46-05:00"`)
// invalid, unparsable index-cache.yaml
indexCacheFilePath = pathutil.Join(suite.TempDirectory, repo.StatefileFilename)
content = []byte(`is this valid yaml? maybe. but its definitely not a valid index.yaml!`)
err = ioutil.WriteFile(indexCacheFilePath, content, 0644)
err = os.WriteFile(indexCacheFilePath, content, 0644)
suite.Nil(err, "no error creating test index-cache.yaml")

NewMultiTenantServer(MultiTenantServerOptions{
Expand Down Expand Up @@ -771,7 +770,7 @@ func (suite *MultiTenantServerTestSuite) extractRepoEntryFromInternalCache(repo

func (suite *MultiTenantServerTestSuite) TestOverwriteServer() {
// Check if files can be overwritten
content, err := ioutil.ReadFile(testTarballPath)
content, err := os.ReadFile(testTarballPath)
suite.Nil(err, "no error opening test tarball")
body := bytes.NewBuffer(content)
res := suite.doRequest("overwrite", "POST", "/api/charts", body, "")
Expand All @@ -792,7 +791,7 @@ func (suite *MultiTenantServerTestSuite) TestOverwriteServer() {
e.RepoLock.RUnlock()
}

content, err = ioutil.ReadFile(testProvfilePath)
content, err = os.ReadFile(testProvfilePath)
suite.Nil(err, "no error opening test provenance file")
body = bytes.NewBuffer(content)
res = suite.doRequest("overwrite", "POST", "/api/prov", body, "")
Expand All @@ -819,14 +818,14 @@ func (suite *MultiTenantServerTestSuite) TestOverwriteServer() {
}

func (suite *MultiTenantServerTestSuite) TestBadChartUpload() {
content, err := ioutil.ReadFile(badTestTarballPath)
content, err := os.ReadFile(badTestTarballPath)
suite.Nil(err, "no error opening test tarball")

body := bytes.NewBuffer(content)
res := suite.doRequest("depth0", "POST", "/api/charts", body, "")
suite.Equal(400, res.Status(), "400 POST /api/charts")

content, err = ioutil.ReadFile(badTestProvfilePath)
content, err = os.ReadFile(badTestProvfilePath)
suite.Nil(err, "no error opening test provenance file")

body = bytes.NewBuffer(content)
Expand All @@ -844,7 +843,7 @@ func (suite *MultiTenantServerTestSuite) TestForceOverwriteServer() {
suite.Equal(200, res.Status(), "200 DELETE /api/charts/mychart/0.1.0")

// Check if files can be overwritten when ?force is on URL
content, err := ioutil.ReadFile(testTarballPath)
content, err := os.ReadFile(testTarballPath)
suite.Nil(err, "no error opening test tarball")
body := bytes.NewBuffer(content)
res = suite.doRequest("forceoverwrite", "POST", "/api/charts", body, "")
Expand All @@ -856,7 +855,7 @@ func (suite *MultiTenantServerTestSuite) TestForceOverwriteServer() {
res = suite.doRequest("forceoverwrite", "POST", "/api/charts?force", body, "")
suite.Equal(201, res.Status(), "201 POST /api/charts?force")

content, err = ioutil.ReadFile(testProvfilePath)
content, err = os.ReadFile(testProvfilePath)
suite.Nil(err, "no error opening test provenance file")
body = bytes.NewBuffer(content)
res = suite.doRequest("forceoverwrite", "POST", "/api/prov", body, "")
Expand Down Expand Up @@ -890,26 +889,26 @@ func (suite *MultiTenantServerTestSuite) TestCustomChartURLServer() {

func (suite *MultiTenantServerTestSuite) TestMaxObjectsServer() {
// Overwrites should still be allowed if limit is reached
content, err := ioutil.ReadFile(testTarballPath)
content, err := os.ReadFile(testTarballPath)
suite.Nil(err, "no error opening test tarball")
body := bytes.NewBuffer(content)
res := suite.doRequest("maxobjects", "POST", "/api/charts", body, "")
suite.Equal(201, res.Status(), "201 POST /api/charts")

content, err = ioutil.ReadFile(testProvfilePath)
content, err = os.ReadFile(testProvfilePath)
suite.Nil(err, "no error opening test provenance file")
body = bytes.NewBuffer(content)
res = suite.doRequest("maxobjects", "POST", "/api/prov", body, "")
suite.Equal(201, res.Status(), "201 POST /api/prov")

// trigger error, reached max
content, err = ioutil.ReadFile(otherTestTarballPath)
content, err = os.ReadFile(otherTestTarballPath)
suite.Nil(err, "no error opening other test tarball")
body = bytes.NewBuffer(content)
res = suite.doRequest("maxobjects", "POST", "/api/charts", body, "")
suite.Equal(507, res.Status(), "507 POST /api/charts")

content, err = ioutil.ReadFile(otherTestProvfilePath)
content, err = os.ReadFile(otherTestProvfilePath)
suite.Nil(err, "no error opening other test provenance file")
body = bytes.NewBuffer(content)
res = suite.doRequest("maxobjects", "POST", "/api/prov", body, "")
Expand All @@ -918,19 +917,19 @@ func (suite *MultiTenantServerTestSuite) TestMaxObjectsServer() {

func (suite *MultiTenantServerTestSuite) TestPerChartLimit() {
ns := "per-chart-limit"
content, err := ioutil.ReadFile(testTarballPathV0)
content, err := os.ReadFile(testTarballPathV0)
suite.Nil(err, "no error opening test tarball")
body := bytes.NewBuffer(content)
res := suite.doRequest(ns, "POST", "/api/charts", body, "")
suite.Equal(201, res.Status(), "201 POST /api/charts")

content, err = ioutil.ReadFile(testTarballPathV2)
content, err = os.ReadFile(testTarballPathV2)
suite.Nil(err, "no error opening test tarball")
body = bytes.NewBuffer(content)
res = suite.doRequest(ns, "POST", "/api/charts", body, "")
suite.Equal(201, res.Status(), "201 POST /api/charts")

content, err = ioutil.ReadFile(testTarballPath)
content, err = os.ReadFile(testTarballPath)
suite.Nil(err, "no error opening test tarball")
body = bytes.NewBuffer(content)
res = suite.doRequest(ns, "POST", "/api/charts", body, "")
Expand All @@ -950,13 +949,13 @@ func (suite *MultiTenantServerTestSuite) TestPerChartLimit() {

func (suite *MultiTenantServerTestSuite) TestMaxUploadSizeServer() {
// trigger 413s, "request too large"
content, err := ioutil.ReadFile(testTarballPath)
content, err := os.ReadFile(testTarballPath)
suite.Nil(err, "no error opening test tarball")
body := bytes.NewBuffer(content)
res := suite.doRequest("maxuploadsize", "POST", "/api/charts", body, "")
suite.Equal(413, res.Status(), "413 POST /api/charts")

content, err = ioutil.ReadFile(testProvfilePath)
content, err = os.ReadFile(testProvfilePath)
suite.Nil(err, "no error opening test provenance file")
body = bytes.NewBuffer(content)
res = suite.doRequest("maxuploadsize", "POST", "/api/prov", body, "")
Expand All @@ -971,14 +970,14 @@ func (suite *MultiTenantServerTestSuite) TestMetrics() {

apiPrefix := pathutil.Join("/api", "a")

content, err := ioutil.ReadFile(testTarballPath)
content, err := os.ReadFile(testTarballPath)
suite.Nil(err, "error opening test tarball")

body := bytes.NewBuffer(content)
res := suite.doRequest("depth1", "POST", fmt.Sprintf("%s/charts", apiPrefix), body, "")
suite.Equal(201, res.Status(), fmt.Sprintf("201 post %s/charts", apiPrefix))

otherChart, err := ioutil.ReadFile(testTarballPathV2)
otherChart, err := os.ReadFile(testTarballPathV2)
suite.Nil(err, "error opening test tarball")

body = bytes.NewBuffer(otherChart)
Expand Down Expand Up @@ -1204,7 +1203,7 @@ func (suite *MultiTenantServerTestSuite) testAllRoutes(repo string, depth int) {
suite.Equal(500, res.Status(), fmt.Sprintf("500 POST %s/prov", apiPrefix))

// POST /api/:repo/charts
content, err := ioutil.ReadFile(testTarballPath)
content, err := os.ReadFile(testTarballPath)
suite.Nil(err, "no error opening test tarball")

body = bytes.NewBuffer(content)
Expand All @@ -1221,7 +1220,7 @@ func (suite *MultiTenantServerTestSuite) testAllRoutes(repo string, depth int) {
suite.Equal(409, res.Status(), fmt.Sprintf("409 POST %s/charts?force", apiPrefix))

// POST /api/:repo/prov
content, err = ioutil.ReadFile(testProvfilePath)
content, err = os.ReadFile(testProvfilePath)
suite.Nil(err, "no error opening test provenance file")

body = bytes.NewBuffer(content)
Expand Down
3 changes: 1 addition & 2 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package config

import (
"fmt"
"io/ioutil"
"os"
pathutil "path"
"testing"
Expand Down Expand Up @@ -50,7 +49,7 @@ basicauth:
`,
)

err := ioutil.WriteFile(tempConfigFile, data, 0644)
err := os.WriteFile(tempConfigFile, data, 0644)
suite.Nil(err, fmt.Sprintf("no error creating new config file %s", tempConfigFile))
suite.TempConfigFile = tempConfigFile
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/repo/chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package repo

import (
"io/ioutil"
"os"
"testing"
"time"

Expand All @@ -35,7 +35,7 @@ type ChartTestSuite struct {

func (suite *ChartTestSuite) SetupSuite() {
tarballPath := "../../testdata/charts/mychart/mychart-0.1.0.tgz"
content, err := ioutil.ReadFile(tarballPath)
content, err := os.ReadFile(tarballPath)
suite.Nil(err, "no error reading test tarball")
suite.TarballContent = content
}
Expand Down